JAVA base (method overrides)

1. What is the method overrides

  • Rewriting: parent child appeared identical method (Note: Return Value Type can be a sub parent)

 

2. The method of rewriting applications:

  • When a subclass needs function of the parent class, subclass and main function has its own unique content and method of the parent class can override. So that followed the functions of the parent class, and defines the type of unique content.

[1] Case

class Demo7_Phone {

    public static void main(String[] args) {

        Ios8 i = new Ios8();

        i.siri();

        i.call();

    }

}



/*

B: Method rewrite applications: 

   * 当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法。这样,即沿袭了父类的功能,又定义了子类特有的内容。

    ios7系统 siri speak English

    ios8系统 siri 说中文

*/





class Ios7 {

    public void call() {

        System.out.println("打电话");

    }





    public void siri() {

        System.out.println("speak English");

    }

}





class Ios8 extends Ios7 {

    public void siri() {

        

        System.out.println("说中文");

        super.siri();

    }

}

 

3. The method of rewriting Notes

[1] Note on rewriting method

  • Parent class private methods can not be overridden

    • Because the parent child class private methods simply can not inherit

  • When subclasses override the parent class method, access rights can not be lower, the best on a consistent

  • Parent class static methods, subclass must be rewritten by the static method

    • In fact, this method is not really rewrite, but the phenomenon is indeed the case, as to why not really overridden methods, polymorphism I will explain (static only cover static)

        

  • Subclasses override the parent class method, the best statement exactly the same.

 

 

4, a method override method overloading and

Difference [1] Override and Overload of? Overload change the return value type?

  • overload may change the type of the return value, look parameter list

  • Method overrides: subclass methods and the emergence of the parent class method declaration exactly the same. The type of the return value, the return value is the same (or child-parent class)

  • Method overloading: the same method names appearing in this class, the list of parameters in different ways. It has nothing to do with the return value type.

 

[2] subclass object method call time:

        * Go first subclass of itself, to find the parent class.

 

5, case

[1] When not using inheritance

class Test3_Person {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }

}

/*

* 使用继承前的学生和老师案例

    * 属性:姓名,年龄

    * 行为:吃饭

    * 老师有特有的方法:讲课

    * 学生有特有的方法:学习

*/





class Student {

    private String name;                    //姓名

    private int age;                        //年龄





    public Student() {}                        //空参构造





    public Student(String name,int age) {    //有参构造

        this.name = name;

        this.age = age;

    }





    public void setName(String name) {        //设置姓名

        this.name = name;

    }





    public String getName() {                //获取姓名

        return name;

    }





    public void setAge(int age) {            //设置年龄

        this.age = age;

    }





    public int getAge() {                    //获取年龄

        return age;

    }





    public void eat() {                        //吃饭

        System.out.println("学生吃饭");

    }





    public void study() {                    //学习

        System.out.println("学生学习");

    }

}





class Teacher {

    private String name;                    //姓名

    private int age;                        //年龄





    public Teacher() {}                        //空参构造





    public Teacher(String name,int age) {    //有参构造

        this.name = name;

        this.age = age;

    }





    public void setName(String name) {        //设置姓名

        this.name = name;

    }





    public String getName() {                //获取姓名

        return name;

    }





    public void setAge(int age) {            //设置年龄

        this.age = age;

    }





    public int getAge() {                    //获取年龄

        return age;

    }





    public void eat() {                        //吃饭

        System.out.println("老师吃饭");

    }





    public void teach() {                    //学习

        System.out.println("老师讲课");

    }

}

[2] After using inheritance

class Test4_Person {

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.setName("张三");

        s1.setAge(23);

        System.out.println(s1.getName() + "..." + s1.getAge());

        s1.eat();

        s1.study();





        System.out.println("------------------");

        Student s2 = new Student("李四",24);

        System.out.println(s2.getName() + "..." + s2.getAge());

        s2.eat();

        s2.study();

    }

}

/*

* 使用继承后的学生和老师案例

*/





class Person {

    private String name;                    //姓名

    private int age;                        //年龄





    public Person() {}                        //空参构造





    public Person(String name,int age) {    //有参构造

        this.name = name;

        this.age = age;

    }





    public void setName(String name) {        //设置姓名

        this.name = name;

    }





    public String getName() {                //获取姓名

        return name;

    }





    public void setAge(int age) {            //设置年龄

        this.age = age;

    }





    public int getAge() {                    //获取年龄

        return age;

    }





    public void eat() {                        //吃饭

        System.out.println(name  + "吃饭");

    }

}





class Student extends Person {

    public Student() {}                        //空参构造





    public Student(String name,int age) {

        super(name,age);

    }

    

    public void eat(){

        System.out.println(this.getName()  + "学生吃饭");

    }





    public void study() {

        System.out.println(this.getName() + "学习");

    }

}





class Teacher extends Person {

    public Teacher() {}                        //空参构造





    public Teacher(String name,int age) {

        super(name,age);

    }

    

    public void eat(){

        System.out.println(this.getName()  + "老师吃饭");

    }

    public void teach() {

        System.out.println(this.getName() + "讲课");

    }

}

 

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/92061345