Article From:https://www.cnblogs.com/MrTanJunCai/p/9906777.html

*********************Chapter VIII Reuse Classes and Polymorphism******************************************************************

Reusable

I. method

II. Reuse Classes

1.combination

2.inherit

*****************************Combination has a********************************************************

I. What is combination?

It’s a special case of relevance.

Combination: Parts can only belong to one whole

The whole has a part.

Human and Heart Cars and Engines

 

Two.How

Class A Combines Objects of Class B

class A {

B b = new B();//combination
}

 

For example, 1:

Students have a girlfriend

public class Student{

private int sno;
private String name;
private int age;

private Girl girl = new Girl();//combination
}

 

For example, 2:

The notebook has a screen.

14Inch screen

package com.chapter8.Demonstration combination;

//14Inch screen
public class Screen14 {

public void display(){
System.out.println(“Use a 14-inch screen for display.
}
}

 

Notebook

package com.chapter8.Demonstration combination;

//Notebook
public class Notebook {

//combination
//has a The notebook has a screen.
private Screen14 screen = new Screen14();

public void work(){
System.out.println(“The motherboard is electrified.
System.out.println(“The hard disk turns to “”.
System.out.println(“cpuRead operating system files from hard disk.
System.out.println(“Fan turn.

screen.display();//Code that reuses screen classes does not need to rewrite the display logic of the screen
}

public static void main(String[] args) {
Notebook notebook = new Notebook();

notebook.work();
}

}

 

3. Why why uses combination

Code for reusing classes

It’s no use using methods to reuse code when there’s the same code in multiple classes

1.It is necessary to extract the same code from multiple classes (fighters, mages, bows and arrows) into one class (Knife knife).

2.When defining a soldier, let the soldier combine a knife and reuse the code of the knife.

//Soldier has a knife
class Soldier {

private Knife knife = new Knife();//combination

public void attack(){

knife.kan();
}
}

The same is true of other classes.

class Archer {

private Knife knife = new Knife();//combination

public void attack(){

knife.kan();
}
}

 

*****************************Can local variables be modified with access control modifiers?****************************************************

No. Because the scope of local variables is determined by the location defined by the programmer, access modifiers cannot be used to modify them.

 

I. Modified Class

2. Modifying the members of a class (1. Attribute 2. Method)

 

 

***********************************Dependence ***************************************************************

Dependency occurs when an object is defined as a local variable and its method is called.

for example

package com.chapter8.Demonstration combination;

rely on
// Laptop Work – ——- gt; Screen Display
public class Notebook {

public void work(){

Screen14 screen = new Screen14();//rely on

System.out.println(“The motherboard is electrified.
System.out.println(“The hard disk turns to “”.
System.out.println(“cpuRead operating system files from hard disk.
System.out.println(“Fan turn.

screen.display();//Code that reuses screen classes does not need to rewrite the display logic of the screen
}

public static void main(String[] args) {
Notebook notebook = new Notebook();

notebook.work();
}

}

**********************************Inheritance of **************************************************************

The second way to reuse classes: inheritance

When does why think of using inheritance

When there are the same methods in multiple classes (eating, sleeping, beating beans), in order to prevent the same methods or attributes in multiple classes

Many times, you can extract the same attributes and methods from multiple classes into a class called Person, which is inherited by other classes.

It solves the problem of code redundancy.

What is inheritance is a

Accurate should be translated into extensions, which can be understood as replication.

The class of student human heir is equivalent to the class of student human expanding code (plus code) on the basis of this class.

 

PersonClasses can be seen as version 0.1 of the code

Subclass Student can be seen as version 0.2 plus code on the basis of 0.1

 

Subclass is a parent

Student is a Person

Teacher is a Person

public class Person {
public void eat() {
System.out.println(“Eating “”;
}

public void sleep() {
System.out.println(“Sleep “”;
}

public void beatBeanBean() {
System.out.println(“”Doudou”.
}
}

 

public class Student extends Person{

public void study(){
//I’m studying
}
}

Amount to

public class Student{
public void eat() {
System.out.println(“Eating “”;
}

public void sleep() {
System.out.println(“Sleep “”;
}

public void beatBeanBean() {
System.out.println(“”Doudou”.
}

public void study(){
//I’m studying
}

}

How to inherit

Use the extends keyword

public class Student extends Person{

public void study(){
//I’m studying
}

public static void main(String[] args) {
Student s = new Student();

s.eat();//Eat () is not defined in the subclass, but it can be invoked to indicate that the eat () method has been inherited from the parent class.
}
}

Person It’s called a parent class, also called a base class, a superclass.

Student It’s called subclass, also called derivative class, derivative class.

 

**Don’t rebuild the wheel. If the parent code has been written once, don’t write it again. Just inherit it directly.

 

***********************************How to choose combination and inheritance? *********************************************************************

Use more combinations and less inheritance. Use inheritance unless there is a clear is a relationship

 

Student is a Person

Why use combination more and inheritance less?

Because composition is more flexible than inheritance

 

*********************Access control rights modifier in inheritance**************************************************************

1.private

privateModified attributes and methods cannot be inherited

2. protected Attributes and methods modified with public can be inherited

 

3. default

Packet jurisdiction

Subclasses in this package can be inherited, and subclasses in other packages cannot be inherited.

 

Examples: Dad’s BMW

 

father

//Parent class
public class Father {

//has a combination
BMW bmw = new BMW();

}

Children

public class Son extends Father{

public void goToPlay(){
bmw.drive();
}

public static void main(String[] args) {
Son son = new Son();
son.goToPlay();
}
}

 

Link of this Article: Java class and polymorphism

Leave a Reply

Your email address will not be published. Required fields are marked *