Case 1.
Package Cn.itcast.oop;public class Thisdemo {public static void main (string[] args) {Student s=new Student (); S.setname ("Lee Formulation "); S.setage (22); String name=s.getname (); int age=s.getage (); SYSTEM.OUT.PRINTLN ("Student's name is:" +name+ " Student's age is:" +age);}} Class Student{private string name;private int age;public string getName () {return name;//this is omitted by default, actually this.name} public void SetName (String name) {name = name;} public int getage () {return age;} public void Setage (int.) {age = Age;}}
This piece of code is simple, but it's easy to make mistakes! The key point of the error is this.
Operation Result:
Why is that? Why the printed result is not--the student's name is: Li Weikang The age of the student is: 22
Case 2.
Package Cn.itcast.oop;public class Studentdemo {public static void main (string[] args) {person s=new person ();}} Class Person{private String name= "Brigitte";p rivate int age=27;public person () {name= "Liu Yi"; age=30;}}
Person s=new person ();
A: Load the Student.class file into memory
B: Open a space in the stack memory for s variables
C: Request a space for human objects in heap memory
D: The default initialization is made to the member. NULL 0
E: Displays initialization of the member variable. Brigitte, 27
F: Initialization of member variables by construction method Liu Yi, 30
G: Data initialization is complete, then the address value of the heap memory is assigned to the s variable of the stack memory
3. Overview of Inheritance
A:java only supports single inheritance of classes does not support multiple inheritance of classes
B: subclasses can only inherit all non-private members of the parent class (member methods and member variables)
C: Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the super (immediate) keyword.
D: the member variable in the subclass is the same as the member variable name in the parent class.
To access the lookup order of a variable in a subclass method:
A: Find the local scope of the subclass method, and use it
B: In the subclass of the member scope to find, there is the use of
C: In the member scope of the parent class, you can use the
D: If you can't find it, you'll get an error.
E:This represents the corresponding reference for this class. Super represents the identity of the parent storage space (which can be understood as a parent class reference that can manipulate members of the parent class)
F: Relationship of construction methods in inheritance
All constructor methods in a subclass access the constructor of the parent's hollow parameter by default: Because the subclass inherits data from the parent class, it may also use the parent class's data. Therefore, before subclasses are initialized, it is important to complete the initialization of the parent class data first. Note: The first statement of each constructor method of a subclass is: Super ();
G: What happens to the constructor of a subclass if the parent does not have a parameterless constructor method? error.
How to solve it?
1): Add a non-parametric construction method to the parent class
2): Call the parent class by using the Super keyword to display the parameter construction method
3 ): Subclasses use this to call other constructors of this class, and there must be one in the subclass that accesses the constructor of the parent class, otherwise the parent data is not initialized.
Precautions:
This (...) or Super (...) Must appear on the first statement.
If it is not placed on the first statement, it is possible to initialize the data of the parent class more than once, so it must be placed on the first statement.
H: Relationship of member methods in Inheritance: 1): The methods in the subclass are not the same as the method declarations in the parent class, this is too simple. 2): The method in the subclass is the same as the method declaration in the parent class, how does this play?
To invoke a method from a subclass object:
A: First look for the sub-class, see if there is this method, there is the use of
B: Again look at the parent class, there is no this method, there is the use of
C: If there is no error.
I: Considerations for Method overrides
1): Private methods in the parent class cannot be overridden-because the parent class private method subclasses simply cannot inherit
2): When subclasses override parent class methods, access permissions cannot be lower--and best consistent
3): Parent class static method, subclass must also be overridden by static method
In fact, this is not the method of rewriting, but the phenomenon is true, as to why not the method rewrite, polymorphic I will say
When a subclass overrides a parent class method, it is best to declare exactly the same.
Examples:
Class Fu {static {System.out.println ("Static code block Fu");} {System.out.println ("Construct code block Fu");} Public Fu () {System.out.println ("construction Method Fu");}} Class Zi extends Fu {static {System.out.println ("Static code block Zi");} {System.out.println ("Construct code block Zi");} Public Zi () {System.out.println ("constructor method Zi");}} Class ExtendsTest2 {public static void main (string[] args) {Zi z = new Zi (); Zi z2 = new Zi ();}}
Execution Result:
See Program Write results:
A: a static block of code for a class, constructing a block of code, and implementing a process for constructing a method
Static code blocks > Constructing code blocks > Construction Methods
B: Static content is loaded as the class loads
The contents of a static block of code take precedence
C: The initialization of the parent class before the subclass is initialized
The result is:
Static code block FU
Static code block Zi
Construct code block FU
Construction Method Fu
Constructing code blocks Zi
Construction Method Zi
Classic Interview Questions:
Class X {x () {System.out.print ("X");} Y B = new Y ();} Class Y {y () {System.out.print ("Y");}} public class Z extends X {z () {//supersystem.out.print ("z");} Y y = new Y ();p ublic static void Main (string[] args) {new Z ();}}
Remember a word: Initialize the parent data first, and then initialize the subclass data
So the execution process is: according to the order of initialization: default initialization, display initialization, construct initialization
1. Initialize the parent class X: Execute member variable to display initialization y y=new y (), so call Y's constructor to print Y
2. Constructor of the parent class is initialized, and code in the construction method is executed to print X
3. Subclass Initializes class Z: executes member variable initialization y y=new y () so call Y's construction method to print Y
4. Constructor initialization for subclasses: Execute code in construction method print Z
The result is: yxyz
Debug mode:
Case:
Package Cn.itcast.extend;public class Demo {public static void main (string[] args) {Zi z=new Zi (); Z.show ();}} Class Fu{int num=10;public void Show () {System.out.println (num);}} Class Zi extends Fu{int num=20;public void Show () {System.out.println (num);}}
The result is no doubt:
Remove the Show method for subclasses
Package Cn.itcast.extend;public class Demo {public static void main (string[] args) {Zi z=new Zi (); Z.show ();}} Class Fu{int num=10;public void Show () {System.out.println (num);}} Class Zi extends Fu{int num=20;}
The answer is: 10
Why: Because the subclass Find method Show () is not found in the parent class to find.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Javase a few simple questions that are most prone to error