Java study notes - Inheritance

inherit

1. inherited the role:

  • The basic role: subclass inherits the parent class
  • Major (important) role: because of inheritance, only the cover and polymorphism method

2. inherited characteristics:

  • java only supports multiple inheritance, but indirect support inheritance
  • Subclass inherits the parent class, in addition to the constructor can not be inherited , the rest can be inherited, but can not directly access private property in the subclass can be accessed by indirect means
    • This inheritance: the parent class subclass inheritance, the parent class is inherited methods for their return at all, then the subclass method is not invoked when the parent class, but its own method.
  • If a class does not inherit any class displays, the default inherit Object class (Java inheritance tree root)

3. When will inherit?

  • Those who adopt "is a" can describe, can inherit
  • Before considering two class inheritance, consider the effect of a combination of two classes
    • For example: cat is Animal Dog is Animal

4. Inherited drawbacks:
coupling high, modified parent subclass implicated

E.g:

//如下将Student,Teacher泛化为Person类:
public class Person{
    private String name;
    private int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
	//置取方法
	public void setName(String name){
		this.name = name;
	}
	public String getName(void){
		return this.name;
	}
	
    public void display(){
        System.out.println("姓名:" + name);
    }
}


Student类:
public class Student extends Person{
    private double grade;

	public Student(String name, int age, double grade){
        super(name, age);
        this.grade = grade;
    }
    public void display(){
        System.out.println("成绩: " + this.grade);
    }
}


public class ExtendsTest01{
	public static void main(String[] args){
		Student s = new Student("Li Si", 23, 100);
                //getName()是子类从父类继承过来的方法
		System.out.println("姓名:" + s.getName())
	}
}

Java source code analysis of inheritance

/** 
* Object.java中
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
*/
public class ExtendsTest02{
	public static void main(String[] args){
		//创建一个对象
                ExtendsTest02 ts = new ExtendsTest02();
		String retValue = ts.toString();
		System.out.println(retValue);
		//如果直接输出"引用"?
		System.out.println(ts);
		
	}
}
![](https://img2020.cnblogs.com/blog/1720775/202004/1720775-20200404213624605-118128102.png)
####分析:
1. ExtendsTest02类默认继承Object类,所以是子类对象调用父类的实例方法。
2. 输出结果"等同"看作调用对象在堆内存中的地址,是实际地址经过“哈希算法”计算出的十六进制地址		
3. 如果直接输出"引用"
- println方法会先自动调用“引用.toString()”
- 然后输出toString()方法的结果
		














### System.out.println

the Test class {public
// static variable
static Student s = new Student () ;

//入口
public static void main(String[] args){
	//System.out.println("Hello");
	//一样的结构
	Test.s.exam();
}

}
Class Student {
// example method
public void Exam (void) {
System.out.println ( "Examination ....");
}

}

Guess you like

Origin www.cnblogs.com/zy200128/p/12634397.html