Oop. Inheritance

Source: Internet
Author: User

Review:
1. Memory Management: JVM
1) Heap: new objects and member variables
Member Variable life cycle:
exists when object is created and disappears when object is recycled
Garbage collector (GC) does not periodically recycle objects that do not have any references to
The recycling process is transparent, fast some system.gc ()
Memory leaks: Objects that are no longer being used are not recycled in a timely manner
Set object references that are no longer in use to NULL in a timely manner
2) Stack: All local variables in the method used
When the method is called, the stack frame corresponding to the method is allocated in the stack,
The stack frame contains the parameters in the method and the local variables in the method,
At the end of the method call, the stack frame disappears and the local variable disappears.
Local variable life cycle:
When the method is called, it disappears at the end of the method call
3) Method Area: Bytecode file and method
Method has only one copy, through this to distinguish which object accesses the
2. Array of reference types:
Cell[] cells = new CELL[4];
Cells[0] = new Cell (n);
CELLS[1] = new Cell (2,3);
CELLS[2] = new Cell (3,4);
CELLS[3] = new Cell (4,5);

Cell[] cells = new cell[]{
New Cell,
New Cell (2,3),
New Cell (3,4),
New Cell (4,5)
};

int[][] arr = new int[3][];
Arr[0] = new INT[2];
ARR[1] = new INT[3];
ARR[2] = new INT[2];
ARR[2][1] = 100; The 2nd element of the 3rd element in Arr is assigned a value of 100

int[][] arr = new INT[3][4];
for (int i=0;i<arr.length;i++) {
for (int j=0;j<arr[i].length;j++) {
ARR[I][J] = 100;
}
}

Class o{
Cell[] cells;
O () {
This (0,0);
}
O (int row,int col) {
Cells = new Cell[4];
Cells[0] = new Cell (row,col);
CELLS[1] = new Cell (row,col+1);
CELLS[2] = new Cell (row+1,col);
CELLS[3] = new Cell (row+1,col+1);
}
void Drop () {
for (int i=0;i<cells.length;i++) {
cells[i].row++;
}
}
void MoveLeft () {
}
void MoveRight () {
}
void print () {
}
}


Notes:
1. Inheritance:
1) Role: Reuse of code
2) implement inheritance through extends
3) Parent class/base class: Variables and methods common to all subclasses
Subclass/Derived class: A method of a variable specific to a subclass
4) When a subclass inherits from the parent class, the subclass has:
The parent class that is unique to the------subclass
5) A class can inherit only one parent class----single inheritance
A parent class can have more than one child class
6) Inheritance has transitivity
7) Java stipulation: The parent class must be constructed before the subclass is constructed
The first sentence of the subclass constructor method has a super () argument-free construction of the parent class by default
If you make a call to the parent class through super, it is no longer provided by default
Super calls the parent class construct and must be in the 1th sentence of the subclass construct
2.super: Refers to the parent class object of the current object
Usage:
1) Super. Member Variable name------Access member variables of the parent class
2) Super. Method Name ()-----------method of calling the parent class
3) Super ()---------------------Call the construction method of the parent class
3. Upward Styling:
1) The reference of the parent type to the object of the child class
2) What you can point out, see the type of reference
4. Override of the method (override):
1) occurs in a parent-child class with the same method name, with the same argument list and different method body
2) When the override method is called, look at the type of the object

Task:
1.print () Override of method
2. See the three demo files in the code
(rewrite, upward styling, super)
3.Cell class, T class, J class, Tetromino class, Tjtest class
--------------rewrite it once
4. After-school assignments: the 3rd day of homework to do 4 and 5
The 4th day's assignment is 1 and 2.
5. Daily Practice

Can point out what, see the type of reference

Main () {
Aoo o = new Boo (); Upward styling
System.out.println (O.A); 8
System.out.println (O.B); Compilation errors,
}


Class aoo{
int a=0;
}
Class Boo extends aoo{
int b;
Boo () {
A = 8;
}
}

Student ZS = new Student ("Zhangsan", 25, "Langfang", "JSD1507");
Zs.sayhi (); zhangsan,25, Langfang, JSD1507

Teacher wkj = new Student ("Wangkj", 37, "Jiamusi", 5000);
Wkj.sayhi (); wangkj,37, Jiamusi, 5000


Class person{
String name;
int age;
String address;
void Sayhi () {
System.out.println (name+ "," +age+ "," +address ");
}
}
Class Doctor extends person{
String level;
}
Class Student extends person{
String ClassName;
void Sayhi () {//rewrite of method (re-write, overwrite)
System.out.println (name+ "," +age+ "," +address+ "," +classname ");
}
}
Class Teacher extends person{
Double salary;
void Sayhi () {
System.out.println (name+ "," +age+ "," +address+ "," +salary ");
}
}


person P1 = new Student ();
person P2 = new Teacher ();
Person P3 = new Doctor ();

Student P4 = new Person (); Compile error

Tetromino O1 = new T (); Upward styling
Tetromino O2 = new J ();
Tetromino O3 = new O ();
Tetromino O4 = new L ();
Tetromino O5 = new I ();
Tetromino O4 = new S ();
Tetromino O5 = new Z ();

A reference to a Tetromino type that can receive a T-type object,
You can also receive a J-type object,
You can also receive an O-type object,
......

Class tetromino{//Parent
}
Class T extends tetromino{//subclass
}
Class J extends tetromino{
}
Class O extends tetromino{
}


Class animal{//Animal
}
Class Tiger extends animal{//Tiger
}

Animals are animals.
Animal O1 = new Animal ();
Tigers are tigers.
Tiger O2 = new Tiger ();
Tigers are animals.
Animal O3 = new Tiger ();
Parent type reference to child type Object

Animals are Tigers--------semantics do not pass
Tiger O4 = new Animal (); Grammar is not allowed.


T-type parameter can only receive T-type Object
J-Type parameters can only receive J-type objects
O-type parameters can only receive O-type objects
-------------want to print 7 kinds of graphics, you must do 7 overloads of the method

Assume:
There is a type that can receive both T-type objects,
can also receive J-type objects,
can also receive O-type objects,
Can also receive L-shaped objects,
......

Parent type


As long as the ranks are up, play *
When 4 * are not in the match, only hit-

Not a single judgment to get the final result------switch

Suppose i=2,j=6

25
26
27
36


Big if------advantages: High Efficiency
Cons: Poor extensibility
Switch-----Disadvantages: low efficiency
Advantages: Good Extensibility

There is also a way to------high efficiency, good scalability


if (tt.cells[0].row==i && tt.cells[0].col==j
||
Tt.cells[1].row==i && tt.cells[01].col==j)

Aoo O1 = new Aoo ();
O1.a/say ();

Boo O2 = New Boo ();
O2.b/show ();
O2.a/say ();

Coo O3 = new COO ();
O3.c/sayhi (); This class
O3.b/show (); Direct Parent Class
O3.a/say (); Indirect parent Class

Class aoo{
int A;
void Say () {}
}
Class Boo extends aoo{
int b;
void Show () {}
}
Class Coo extends boo{
int C;
void Sayhi () {}
}

Student ZS = new Student ();
Zs.classname/study ();
Zs.name/age/address/eat ()/sleep ();


Class person{//Human
String name;
int age;
String address;
void Eat () {}
void Sleep () {}
}
Child class Parent Class
Class Student extends person{
String ClassName;
void study () {}
}
Class Teacher extends person{
Double salary;
void Teach () {}
}
Class Doctor extends person{
String level;
void Cut () {}
}
Class t{
}
Class j{
}
Class o{
}
Class l{
}

Tetromino

The Inheritance in life:
1. Inheritance of property:
Money does not need to earn its own, can also spend
2. Succession to the throne:
Jiangshan does not need to fight itself, can also sit
3. Succession work:
You don't need to find your job, you can do it yourself.

Oop. Inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.