Study notes: polymorphism and inner classes in java

Polymorphism

l Polymorphism is the most important concept in object-oriented. There are two manifestations in Java:

1. Method overload (overload) and rewrite (overwrite).

2. Object polymorphism -can be directly applied to abstract classes and interfaces.

Java reference variables have two types: compile-time type and runtime type . The compile-time type is determined by the type used when the variable is declared, and the runtime type is determined by the object actually assigned to the variable.

If the run time and compile-time type inconsistent type, there have polymorphism ( Polymorphism ) L
polymorphic object - in Java, an object subclass can replace the object using the parent class

A variable can only have a certain data type

A reference type variable may point to (reference) many different types of objects

Person p = new Person();

Person e = new Student(); // Variable e of type Person points to an object of type Student

It can be seen as a special sub-class of the parent class, so the reference to the parent class type of the object subclass can point: upward transition ( upcasting ).
If a reference type variable is declared as the type of the parent class, but actually refers to the subclass object, then the variable can no longer access the properties and methods added in the subclass

Student m = new Student();

m.school = " pku "; // Legal , the Student class has school member variables

Person e = new Student();

e.school = " pku "; // Illegal , the Person class has no school member variable

The attributes are determined at compile time. At compile time, e is of type Person , and there is no school member variable, so the compilation error occurs .

Inner class

In Java, the definition of a class is allowed to be located inside another class. The former is called an inner class and the latter is called an outer class.

Inner class is generally used in the class or statement block in which it is defined, and the complete name must be given when referencing it externally.

The name of the Inner class cannot be the same as the name of the class containing it;

Inner class can use the private data of the outer class, because it is a member of the outer class, and members of the same class can access each other. The outer class needs to access the members of the inner class, inner class. member or inner class object. member.

Classification: member inner class (static member inner class and non-static member inner class)

Partial inner class (aside modifier), anonymous inner classes

 class A {
    
    

 private int s;

 public class B{
    
    public void mb() {
    
    

 s = 100;  

 System.out.println("在内部类B中s=" + s);} }

 public void ma() {
    
    

 B i = new B();

 i.mb();

 } }public class Test {
    
     

 public static void main(String args[]){
    
    

​     A o = new A();

​     o.ma();

 }  } 

Inner class**** as a member of the class:

It can be declared as final the

Unlike external classes, Inner class can be declared as private or protected ;

Inner class can be declared as static , but non-static member variables of the outer class can no longer be used at this time;

Inner class as a class:

Can be declared as an abstract class, so it can be inherited by other inner classes

[Note] Members of non-static inner classes cannot be declared as static, and static members can only be declared in outer classes or static inner classes.
An anonymous inner class cannot define any static members, methods, and classes, only one instance of the anonymous inner class can be created. An anonymous inner class must be behind new, and use it to implicitly implement an interface or implement a class.

new parent class constructor (argument list)|implementation interface(){

//The class body part of the anonymous inner class

}

interface A{
    
    

 public abstract void fun1();

}

public class Outer{
    
    

 public static void main(String[] args) {
    
    

 new Outer().callInner(new A(){
    
    //接口是不能new但此处比较特殊是子类对象实现接口,只不过没有为对象取名

 public void fun1() {
    
    

 System.out.println(“implement for fun1");

 }

 });// 两步写成一步了

 }

 public void callInner(A a) {
    
    

 a.fun1();

 }

} 

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/105414167