-
Polymorphism is special when it comes to domains. I can't understand that the Chinese version of the book is directly called domain. I read the original English version, and the original version is fields. Although the direct translation is correct, the variable in question is not the domain. Specifically checked what is the meaning of field in java? Many people think of it as a range enclosed by curly braces. In fact, there is also a thing in the framework - domain model, which is also called domain, domain model.
then find this article
What is a field in java?
A field is an attribute. A field may be a class's variable, an object's variable, an object's method's variable, or a parameter of a function.
- class bike{
- static int bikes;
- int gear;
- int cadence;
- void create( int newGear, int newCadence ){
- bikes = bikes + 1;
- gear = newGear;
- cadence = newCadence;}
- int getSpeed(){
- int speed = gear*cadence*5*3.141;
- return speed;
- }
- }
'bikes' is a class's variable (class variable) (static field).
'gear' and 'cadence' could be an object's variables (instance variables) (non-static fields).
'speed' is an object's method's variable (local variable).
'newGear' and 'newCadence' are parameters of a function (parameters).
field is an attribute that can be a class variable, an object variable, an object method variable or a function parameter. (Additionally, class's variables, class instance variables and static variables are called class's variables, generic variables, also known as class variables or data fields, in fact, they can also be translated into attributes, class attributes, it doesn't sound weird, from Baidu encyclopedia).
- class bike{
- static int bikes;
- int gear;
- int cadence;
- void create( int newGear, int newCadence ){
- bikes = bikes + 1;
- gear = newGear;
- cadence = newCadence;}
- int getSpeed(){
- int speed = gear*cadence*5*3.141;
- return speed;
- }
- }
bikes is a class variable (static field).gear and cadence are object variables (instance variables) (non-static fields).
(There is a little contradiction here. In fact, according to Encyclopedia, bikes, gear, and cadence are all class variables, bikes are static variables in class variables, and gear and cadence are instance variables in class variables.)
speed is a variable (local variable) of the object method.
(See no, local variable, java does not have global variable, global variable, if you want to say, the scope of the class variable is the same as the global variable, but it is not called that).
newGear and newCadence are parameters (parameters) of a function (method).
- **
- * public class, visible in all packages
- * uthorauthor OOS
- *
- */
- public class PublicClass {
- /**
- * public domain, visible in all subclasses
- */
- public int publicNum;
- /**
- * Private field, visible to the current class
- */
- private int privateNum;
- /**
- * Protected domain, visible in all subclasses
- */
- protected int protectedNum;
- /**
- * Default domain, visible in subclasses of the current package
- */
- int defaultNum;
- /**
- * public method
- * Available: All classes
- * Inheritable: all subclasses
- */
- public void publicMethod(){};
- /**
- * private method
- * Available: current class
- * Inheritable: none
- */
- private void privateMethod(){};
- /**
- * Protection method
- * Available in: current package
- * Inheritable: all subclasses
- */
- protected void protectedMethod(){};
- /**
- * default method
- * Available in: current package
- * Inheritable: subclasses of the current package
- */
- void defaultMethod(){};
- /**
- * Private class, only visible in the class in which it is defined
- * uthorauthor OOS
- *
- */
- private class PrivateClass{
- }
- /**
- * Protected class, visible only in current package, current class and all subclasses
- * uthorauthor OOS
- *
- */
- protected class ProtectedClass{
- protected ProtectedClass()
- {
- }
- }
- /**
- * Default class, only visible in the current package
- * uthorauthor OOS
- *
- */
- class DefaultClass {
- }
- }
The test class CurrentPackageTest in the same package as PublicClass is as follows:
- public class CurrentPackageTest extends PublicClass{
- public static void main(String[] args)
- {
- PublicClass pc = new PublicClass();
- //The current package can use protected methods and default methods
- pc.protectedMethod();
- pc.defaultMethod();
- //The protected class is visible in the current package
- PublicClass.ProtectedClass protectedClass = new PublicClass().new ProtectedClass();
- //The default class is visible in the current package
- DefaultClass defaultClass = new PublicClass().new DefaultClass();
- }
- public CurrentPackageTest()
- {
- System.out.println(super.publicNum);
- System.out.println(super.protectedNum);
- //PublicClass and the current class belong to the same package, so defaultNum can be inherited
- System.out.println(super.defaultNum);
- }
- }
Different from PublicClass, the test class OtherPackageTest in a package is as follows:
- public class OtherPackageTest extends PublicClass{
- public static void main(String[] args)
- {
- PublicClass pc = new PublicClass();
- //PublicClass does not belong to the same package as the current class, only public methods can be used
- pc.publicMethod();
- }
- public OtherPackageTest()
- {
- System.out.println(super.publicNum);
- System.out.println(super.protectedNum);
- }
- }