继承/多态

每日一练

递归求汉诺塔 

package day0801;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("玩几层汉诺塔:");

        int i = new Scanner(System.in).nextInt();

        f(i);

    }

 

    private static void f(int i) {

        f(i,"A","B","C");

    }

 

    private static void f(int i, String a, String b, String c) {

        if(i==1) {

            System.out.println(a+"->"+c);

            return;

        }

        //n-1 A->C->B

        f(i-1,a,c,b);

        System.out.println(a+"->"+c);

        //n-1 b->a->c

        f(i-1,b,a,c);

    }

}

继承

目的:代码重用

单继承

不继承:构造方法/私有成员

子类对象:调用成员时,先子类后父类

 

方法重写:继承的方法在子类中重新编写

父类构造方法:super()必须是首行代码

super.xxx():调用父类成员

 

电子宠物

package day0802;

 

import java.util.Random;

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.println("1.猫");

        System.out.println("2.狗");

        System.out.print("选择:");

        int i = new Scanner(System.in).nextInt();

 

        System.out.print("给宠物起个名字");

        String s = new Scanner(System.in).nextLine();

 

        Dog dog = null;

        Cat cat = null;

        if(i == 1){

            cat = new Cat(s);

            play(cat);

        }else if(i == 2){

            dog = new Dog(s);

            play(dog);

        }

    }

 

    private static void play(Cat cat) {

        System.out.println("按回车继续");

        while (true)

        {

            int i = new Random().nextInt(3);

            switch (i)

            {

                case 0: cat.feed();break;

                case 1: cat.punish();break;

                case 2: cat.play();break;

            }

            new Scanner(System.in).nextLine();

        }

    }

 

    private static void play(Dog dog) {

        System.out.println("按回车继续");

        while (true)

        {

            int i = new Random().nextInt(3);

            switch (i)

            {

                case 0: dog.feed();break;

                case 1: dog.punish();break;

                case 2: dog.play();break;

            }

            new Scanner(System.in).nextLine();

        }

    }

}

 

package day0802;

 

public class Pet {

    String name;

    int full;

    int happy;

 

    public Pet(String name)

    {

        this(name,50,50);

    }

 

    public Pet(String name,int full,int happy)

    {

        this.full = full;

        this.happy = happy;

        this.name = name;

    }

 

    public void feed()

    {

        if(full > 90)

        {

            System.out.println(name+"已经吃饱了");

            return;

        }

        System.out.println("给"+name+"喂食");

        full+=10;

        System.out.println("饱食度:"+full);

    }

 

    public void play()

    {

        if(full <= 0)

        {

            System.out.println(name+"玩不动了");

            return;

        }

        System.out.println("陪"+name+"玩耍");

        full-=10;

        happy+=10;

        System.out.println("饱食度:"+full+"快乐度:"+happy);

    }

 

    public void punish()

    {

        //TODO:要能让不同宠物有不同的行为

        System.out.println("惩罚"+name+cry());

        happy-=10;

        System.out.println("快乐度:"+happy);

    }

 

    public String cry() {

        return "";

    }

}

 

package day0802;

 

public class Cat extends Pet {

    public Cat(String name) {

        super(name);

    }

 

    public Cat(String name, int full, int happy) {

        super(name, full, happy);

    }

 

    @Override

    public String cry()

    {

        return "喵喵喵";

    }

}

 

package day0802;

 

public class Dog extends Pet {

 

    public Dog(String name) {

        super(name);

    }

 

    public Dog(String name, int full, int happy) {

        super(name, full, happy);

    }

 

    @Override

    public String cry()

    {

        return "汪汪汪";

    }

}

object是所有类的顶层父类

objecttoString()方法返回:类名@内存地址值

 

多态

所用:一致的类型

子类型对象,可以被当作父类型来处理

节省代码

类型转换:

向上转型:子类的对象转型为父类型

向下转型:已经转成父类型的子类对象,再转回成子类型

instanceof

运行期类型识别

对真实类型和父类型判断,都得到 true

Shape s = new Line();

instanceof Line     true

instanceof Shape    true

instanceof Object   true

 

package day0705;

 

public class Test1 {

    public static void main(String[] args) {

        Person person = new Person("张三", 22,"男");

        Student student = new Student();

        Employee employee = new Employee();

 

        student.name = "李四";

        student.gender = "女";

        student.age = 18;

        student.school = "家里蹲大学";

 

        employee.name = "王五";

        employee.gender = "女";

        employee.age = 17;

        employee.salary = 12000;

 

        /*System.out.println(person.toString());

        System.out.println(student.toString());

        System.out.println(employee.toString());*/

 

        f(person);

        f(student);

        f(employee);

 

    }

 

    static void f(Person p)

    {

        System.out.println(p.toString());

    }

}

 

package day0705;

 

public class Test3 {

    public static void main(String[] args) {

        Student s = new Student();

        Person p = s;

    }

}