这篇文章纯粹是个人笔记,感觉不重要但是知识零碎,自己可能过几天就忘了,所以发到网站上来闲着没事自己偶尔看看,复习一�?

练习

这个程序我就不写了,相对来说简单一�?

成员方法被继承的条件

继承原则

继承访问特点

0x01成员变量访问特点

就近原则:先从本类的局部位置找,然后再去本类成员位置中找,然后再到父类成员位置�?

0x02成员方法的访问特�?

同成员变量访问特点,就近原则

方法重写:当父类中的方法不满足子类的需求的时候,可以写一个跟父类中方法名一样的方法来覆盖子类中的方�?

虚方法表:非private 非static 非final修饰的方法可以加到虚方法表中

0x03构造方法的访问特点

如果想调用父类中的有参构造,必须手动书写来进行调用,例如下方例子

0x04练习

public class employs {
    private int number;
    private String name;
    private int money;

    public employs() {
    }

    public employs(int number, String name, int money) {
        this.number = number;
        this.name = name;
        this.money = money;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public void work(){
        System.out.println("管理其他�?");
    }
    public void eat(){
        System.out.println("吃米�?");
    }
}
public class manager extends employs {

    private int bonus;

    public manager() {
    }

    public manager(int number, String name, int money, int bonus) {
        super(number, name, money);
        this.bonus = bonus;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}
public class cook extends employs {

    public cook() {
    }

    public cook(int number, String name, int money) {
        super(number, name, money);
    }

    @Override
    public void work() {
        System.out.println("炒菜");
    }
}
package heimap128;


public class test {
    public static void main(String[] args) {
        manager ma = new manager(1, "张三", 10000, 500);
        System.out.println(ma.getNumber() + ", " + ma.getName() + ", " + ma.getMoney() + ", " + ma.getBonus());
        System.out.print(ma.getName() + "�?");
        ma.work();
        ma.eat();

        cook ck = new cook(2,"李四",5000);
        System.out.println(ck.getNumber() + ", " + ck.getName() + ", " + ck.getMoney());
        System.out.print(ck.getName() + "�?");
        ck.work();
        ck.eat();


    }
}

this和super关键�?

多�?

0x01概念性问�?

0x02多态中调用成员的特�?

关于调用成员的特点,我简单写个例�?

public class Test {
    public static void main(String[] args) {
        Animal dog = new Dog();

        //调用成员变量:编译看左边,运行也看左�?
        //编译看左边:javac编译代码的时候,会看左边的父类中有没有这个变量,如果有编译成功,如果没有,编译失�?
        //运行看左边:java运行代码的过程时候,实际获取的就是左边父类成员变量的�?
        System.out.println(dog.name);//所以会输出父类中的name动物

        //调用成员方法:编译看左边,运行看右边
        //编译看左边:javac编译代码的时候,会看左边的父类中有没有这个方法,如果有编译成功,如果没有,编译失�?
        //运行看右边:java运行代码的过程时候,实际运行的方法是子类中的方法
        dog.show();//输出的是Dog.show
    }
}
class Animal{
    String name = "动物";

    public void show(){
        System.out.println("Animal.show");
    }
}
class Dog extends Animal{
    String name = "Dog";

    @Override
    public void show() {
        System.out.println("Dog.show");
    }
}

0x03多态的优势和弊�?

public class Test {
    public static void main(String[] args) {
        //多态方式创建对�?
        Animal a = new Dog();
        a.eat();//可以正常调用在父类中的方法eat()

        //弊端:无法调用子类的特有方法,例如a.lookHome
        //原因:编译时先检查父类中有无次方法,父类中没有直接报�?

        a.lookHome();//将会报错,因为父类Animal中没有这个方法,无法调用子类中特有的方法

        //解决办法: 对创建的对象进行类型强转,即可调用子类特有方�?
        Dog d = (Dog) a;
        d.lookHome();

        //jdk14出现的新特�?
        //判断a是否为Dog类型,如果是,则强转为Dog类型,转换后变量名为d
        //如果不是,则不强转,结果直接是false
        if(a instanceof Dog d){
            d.lookHome();
        }else if(a instanceof Cat c){
            c.catchMouse();
        }else{
            System.out.println("没有这个类型,无法转�?");
        }
    }
}

class Animal{

    public void eat(){
        System.out.println("动物在吃东西");
    }
}

class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫在吃小鱼干");
    }
    public void catchMouse(){
        System.out.println("猫抓老鼠");
    }
}

class Dog extends Animal{

    public void lookHome(){
        System.out.println("狗看�?");
    }
    @Override
    public void eat() {
        System.out.println("狗在吃骨�?");
    }
}

0x04多态练�?

public class Test {
    public static void main(String[] args) {
        Person p1 = new Person("老王",30);
        Dog d = new Dog(2,"�?");
        p1.keepPet(d,"骨头");
        System.out.println();


        Person p2 = new Person("老李",25);
        Cat c = new Cat(3,"�?");
        p2.keepPet(c,"�?");
    }
}
class Animal {
    private int age;
    private String color;

    public Animal() {
    }

    public Animal(int age, String color) {
        this.age = age;
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void eat(String something) {
        System.out.println("动物在吃" + something);
    }
}
class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void keepPet(Animal a, String something) {
        if(a instanceof Dog) {
            Dog d = (Dog) a;
            System.out.println("年龄�?" + age + "�?" + name + "养了一�?" + a.getColor() + "颜色�?" + a.getAge() + "岁的�?");
            d.eat(something);
            d.lookHome();
        }else if(a instanceof Cat){
            Cat c = (Cat) a;
            System.out.println("年龄�?" + age + "�?" + name + "养了一�?" + a.getColor() + "颜色�?" + a.getAge() + "岁的�?");
            c.eat(something);
            c.catchMouse();
        }
    }
}
class Dog extends Animal {


    public Dog() {
    }

    public Dog(int age, String color) {
        super(age, color);
    }


    public void lookHome() {
        System.out.println("狗看�?");
    }

    @Override
    public void eat(String something) {
        System.out.println(getAge() + "岁的" + getColor() + "颜色的狗在吃" + something);
    }
}
class Cat extends Animal {

    public Cat() {
    }

    public Cat(int age, String color) {
        super(age, color);
    }


    @Override
    public void eat(String something) {
        System.out.println(getAge() + "岁的" + getColor() + "颜色的猫在吃" + something);
    }

    public void catchMouse() {
        System.out.println("猫抓老鼠");
    }
}