JAVA basis of a simple review (b)

2020 March 2

Inheritance (then yesterday)

  • N: Inherited face questions
  • Look procedures write results 1
class Fu{
	public int num = 10;
	public Fu(){
		System.out.println("fu");
	}
}
class Zi extends Fu{
	public int num = 20;
	public Zi(){
		System.out.println("zi");
	}
	public void show(){
		int num = 30;
		System.out.println(num);
		System.out.println(this.num);
		System.out.println(super.num);
	}
}
class Test1_Extends {
	public static void main(String[] args) {
		Zi z = new Zi();
		z.show();
	}
}

结果:
fu
zi
30
20
10
  • Look program writes Results
class Fu {
			static {
				System.out.println("静态代码块Fu");
			}
	
			{
				System.out.println("构造代码块Fu");
			}
	
			public Fu() {
				System.out.println("构造方法Fu");
			}
		}
	
		class Zi extends Fu {
			static {
				System.out.println("静态代码块Zi");
			}
	
			{
				System.out.println("构造代码块Zi");
			}
	
			public Zi() {
				System.out.println("构造方法Zi");
			}
		}
	
		Zi z = new Zi(); 请执行结果。


解析:
1,jvm调用了main方法,main进栈
2,遇到Zi z = new Zi();会先将Fu.class和Zi.class分别加载进内存,再创建对象,当Fu.class加载进内存父类的静态代码块会随着Fu.class一起加载,当Zi.class加载进内存,子类的静态代码块会随着Zi.class一起加载第一个输出,静态代码块Fu,第二个输出静态代码块Zi
3,走Zi类的构造方法,因为java中是分层初始化的,先初始化父类,再初始化子类,所以先走的父类构造,但是在执行父类构造时,发现父类有构造代码块,构造代码块是优先于构造方法执行并且在调用构造时执行,所以第三个输出构造代码块Fu,第四个输出构造方法Fu
4,Fu类初始化结束,子类初始化,第五个输出的是构造代码块Zi,构造方法Zi
结果:
静态代码块Fu
静态代码块Zi
构造代码块Fu
构造方法Fu
构造代码块Zi
构造方法Zi
  • O: Overview rewrite method and its application
    • a: What is overridden method?
      • Rewriting: parent child appeared identical method (return type can be a sub parent)
    • B: Method rewrite 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 you can override. So that it followed the functions of the parent class, and defines the type of unique content.
    • c: Notes on rewriting method
      • a: the parent class private methods can not be overridden
        • Because the parent child class private methods simply can not inherit
      • a b: subclass overrides the superclass method, access can not be less
        • It is consistent with the best
      • c: static methods parent class, subclass must also be rewritten by a static method
        • In fact, this method is not really rewrite, but the phenomenon is true. (Static only cover static)
        • Subclasses override the parent class method, the best statement exactly the same.
    • d: interview questions:
      * 1:? The difference Override and Overload Overload change the return value type?
      * A: Overload is overloaded, Override is rewriting method. Rewrite: 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. Subclass object method call, first call the subclass, find the parent class.
  • P: Final
    • 1: final Overview
      • final is the ultimate meaning of the keyword modified class.
    • 2: final modifications Features
      • Modified class, the class can not be inherited
      • Modifying a variable, it becomes a constant, it can only be assigned once
      • Modification methods, the method can not be overridden
    • 3: final keyword modified local variables
      • Basic types, a value can not be changed
      • Reference type, the address value can not be changed, object properties may be varied
    • 4: final modified variable initialization timing
      • Display initialization
      • Before he can object construction is completed

Today on the first review here.

Released nine original articles · won praise 8 · views 387

Guess you like

Origin blog.csdn.net/qq_41816516/article/details/104617996