(A recap of last season's content:
1. super and this keywords
2. Method overloading and overriding
Top knowledge points this season:
1. final keywords
2. Simple understanding of general classes and interfaces (the most important part of JAVA)
final keyword
finalizer - final
1. A class marked as final cannot be inherited
finalclassA
{
};
classBextendsA
{
};
Verify it:
2. The method marked as final cannot be overridden by subclasses
finalclassA
{
publicfinalvoidprint(){}
};
classBextendsA
{
publicvoidprint(){}
};
3. The variable symbolized by final becomes a constant. If it becomes a constant, it cannot be modified later
classA
{
finalStringNAME=Hello;
publicfinalvoidprint()
{
NAME=World;
}
};
When declaring variables before, the first letter of the first word is lowercase, and the first letter of each word is uppercase. If you use final to declare constants, all words must be capitalized.
Focus: Big Picture Constants:
static: shared by all targets
final: is a constant
public: indicates that it can be seen by the outside world
publicstaticfinalStringFLAG=redking.blog.51cto;Overall constant
general class
Generic class: A class that includes a generic method is called a generic class.
General method: A method that is only declared but not implemented is called a general method.
A method without a method body (method body: "{}") is called a general method.
Except for the general method, the other definitions are the same as the general class.
General class = function of general class general method
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//There is a method body, so it is a general method
System.out.println(HelloWorld~~~);
}
//A general method is defined here
publicabstractvoidfun();
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Aa=newA();
}
};
If you want to use a general class, you can't instantiate it directly, it is necessary for the general class to have subclasses.
The general class must be inherited. If the inherited subclass is not a general class, it must override the comprehensive general method.
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//There is a method body, so it is a general method
System.out.println(HelloWorld~~~);
}
//A general method is defined here
publicabstractvoidfun();
};
//The subclass overrides the comprehensive general method in the general class
classBextendsA
{
publicvoidfun()
{
//super.FLAG can also be written as FLAG, because FLAG is now an overall constant ha~~~
System.out.println(FLAG= super.FLAG);
}
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.fun();
b.print();
}
};
Verify the effect and prove that it is necessary to write the general class like this~~~
Generic class definition
General class usage rules
General Class Considerations
abstractclassPerson
{
//Person class should have name and age
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//Define an output method, but this method is a general method
publicabstractStringgetInfo();
};
Let's test it and find that the compilation is normal, indicating that the general class can have structural methods~
We will continue~
abstractclassPerson
{
//Person class should have name and age
privateStringname;
privateintage;
publicPerson(){}
//If it is not already parameterless, it is necessary to explicitly call the parameterless structure in the subclass
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//Define an output method, but this method is a general method
publicabstractStringgetInfo();
};
classStudentextendsPerson
{
publicStudent(Stringname,intage)
{
//Call the structure method with two parameters in the Person class
super(name, age);
}
//Override the general method of the parent class
publicStringgetInfo()
{
return name: super.getName() , age: super.getAge();
}
};
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
Students=newStudent(Wang Qian,27);
System.out.println(s.getInfo());
}
}
The general class is allowed to have structural methods, but this structural method is not used to directly instantiate the general class's own goal. If there is no clear no-parameter structural method in the general class, that is: there is a parameterized structure, it is necessary to The class clearly uses super to indicate which structural method in the parent class to call.
Notice:
If a general class does not have any general method, it cannot be instantiated directly.
abstractclassA
{
publicvoidprint(){}
};
publicclassDemo06
{
publicstaticvoidmain(Stringargs)
{
newA();
}
};
Final can declare a class, but this class must not have subclasses.
The general class must be inherited by subclasses. ==gt;opposite
finalabstractclassA
{)