preface
This article is compiled from the notes in the process of personal learning and review, which is suitable for readers to get started and review quickly.
1, Classes and objects
Class 1.1
A class is a class. A class creation object is our commonly used new object
1.2 reference type
In addition to the eight basic types of data types in JAVA, there are also reference types.
What is a reference?
If the type of a variable is a class, the variable is a reference. It can also be said that this variable points to a class.
Reference type refers to the data type of a variable, which is a class.
The encapsulation type of the basic type is actually a reference type.
The concepts quoted are more important. You can query, digest and understand more when you have time.
package BasicDemo07_ClassAndObject; /** * This class is used to test * @author Sharry * For learning * */ public class Dragon { //Attributes private int blood; private String attack; //Method public void eat() { System.out.println("Eating"); } public static void main(String[] args) { //create object new Dragon(); //The reference points to the newly created object Dragon d1 = new Dragon(); System.out.println(d1); //Test pointed to by reference Dragon d2 = new Dragon(); Dragon d3 = new Dragon(); Dragon d4 = d2; Dragon d5 = d3; //Dragon d4 = d3; //This is illegal. You can only point to one object System.out.println(d2); System.out.println(d4); } }
1.3 class inheritance
Inheritance is one of the three characteristics of object-oriented. Inheritance improves code reusability.
JAVA is single inheritance. A class can only have one parent class.
public class Father { //Define attribute int age; String name; //Define method public void action(){ System.out.println("I am super class"); } } class Son extends Father{ public void action(){ System.out.println("I am son class"); } } class test { public static void main(String[] args){ Son son = new Son(); son.age = 1; son.name = "2B"; son.action(); }
Note that when the parent class has a class method, that is, a static modified method, the child class will hide the method. The static modification method can be directly clicked through the class name. The following is an example:
public class HideMethodFather { //Attributes private int age; private String name; //Class static method public static void eat() { System.out.println("Father is eating"); } } public class HideMethodSon //Attributes private int sonNum; //Hide method public static void eat() { System.out.println("Son is eating"); } public static void main(String[] args) { HideMethodFather.eat(); HideMethodSon.eat(); }
1.4 access modifier
public class Review_Jurisdiction { int Num; //Nothing - friendly public String Name; //Public attribute private String Wife; //Private property protected long length; //Protection properties void Firendly(){ System.out.println("Test the friendly method"); } //amicable means public void Methodtest_01(){ System.out.println("Test the public method"); }; //Public method private void Methodtest_02() { System.out.println("Test the private method"); } //Private method protected void Method_03() { System.out.println("Test the method of protection"); } //Protection method }
Modifier access table:
Modifier | Same class | Same package | Subclasses of different packages | Non subclasses of different packages |
---|---|---|---|---|
private | √ | |||
default | √ | √ | ||
protected | √ | √ | √ | |
public | √ | √ | √ | √ |
1.5 construction method
Creating an object from a class is called instantiation
Instantiation is achieved by calling construction methods (also known as constructors).
We can pass parameters to objects through parametric construction.
Syntax rule of construction method: method name = class name, no return value.
public class Monster { //aggressivity private int attackPower; //Defensive power private int defendPower; //Number of stars private int starLevel; //effect private String funtion; //action public String attack() { return "Attacking!"; } //Construction method public Monster(int attackPower, int defendPower, int starLevel, String funtion) { super(); this.attackPower = attackPower; this.defendPower = defendPower; this.starLevel = starLevel; this.funtion = funtion; } public Monster() { //Parameterless construction, where super calls the method of the parent class Object. Everything is an Object! super(); } }
When no constructor is written on this class, there is a parameterless constructor by default. When we write the construction method, the original parameterless construction will be overwritten by default.
1.6 overloading and rewriting of methods
In JAVA, the method of duplicate name has two concepts: overloading and rewriting.
Overloading usually occurs in the same class, indicating the same method name and different parameters.
Rewriting usually occurs in the inheritance relationship, which means that the subclass rewrites the method with the same name as the parent class.
1.6.1 heavy load
Overload refers to the same method name and different parameters. The reusability of code is improved.
//super keyword to call the parent class method public void testSuper() { super.attack(); } //Overloading of methods public void testSuper(int j) { System.out.println(j); }
1.6.2 rewriting
public class Monster { //aggressivity private int attackPower; //Defensive power private int defendPower; //Number of stars private int starLevel; //effect private String funtion; //action public String attack() { return "Attacking!"; } //Construction method public Monster(int attackPower, int defendPower, int starLevel, String funtion) { super(); this.attackPower = attackPower; this.defendPower = defendPower; this.starLevel = starLevel; this.funtion = funtion; } public Monster() { //Parameterless construction, where super calls the method of the parent class Object. Everything is an Object! super(); } } class BlueEyesWhiteDragon extends Monster{ //The name of blue eyed white dragon is fixed and cannot be modified, so it is modified by final. Final can also be used with static private static final String name = "BlueEyeWhiteDragon"; //super keyword to call the parent class method public void testSuper() { super.attack(); } //This is the rewriting of the method, annotated with @ Override annotation @Override public String attack() { return "Blast of destruction"; } }
1.7 Keywords: this, static, final, super
These keywords are commonly used in classes.
1.7.1 this
this refers to the class "self". We see it in setter s.
public int getAttackPower() { return attackPower; } public void setAttackPower(int attackPower) { this.attackPower = attackPower; }
1.7.2 static
Static means static. You can modify methods, attributes and blocks (blocks are the code wrapped in braces {}).
Execution order: static block > main() > construction block > construction method.
Methods that are not modified by static can call static methods. Static methods cannot directly call methods that are not modified by static.
The method modified by static can be called directly with [class name. Method name].
1.7.3 final
Final translation is the final meaning, so what is modified by final is usually a constant and cannot be modified.
//The name of blue eyed white dragon is fixed and cannot be modified, so it is modified by final. Final can also be used with static private static final String name = "BlueEyeWhiteDragon";
1.7.4 super
super represents the method of the subclass calling the parent class.
These keywords are simply reflected in the code:
package BasicDemo07_ClassAndObject; /** * @author Sharry * This class is used to make an example for learning * */ public class Monster { //aggressivity private int attackPower; //Defensive power private int defendPower; //Number of stars private int starLevel; //effect private String funtion; //action public String attack() { return "Attacking!"; } //Construction method public Monster(int attackPower, int defendPower, int starLevel, String funtion) { super(); this.attackPower = attackPower; this.defendPower = defendPower; this.starLevel = starLevel; this.funtion = funtion; } public Monster() { //Parameterless construction, where super calls the method of the parent class Object. Everything is an Object! super(); } public int getAttackPower() { return attackPower; } public void setAttackPower(int attackPower) { this.attackPower = attackPower; } public int getDefendPower() { return defendPower; } public void setDefendPower(int defendPower) { this.defendPower = defendPower; } public int getStarLevel() { return starLevel; } public void setStarLevel(int starLevel) { this.starLevel = starLevel; } public String getFuntion() { return funtion; } public void setFuntion(String funtion) { this.funtion = funtion; } } public class BlueEyesWhiteDragon extends Monster { //The name of blue eyed white dragon is fixed and cannot be modified, so it is modified by final. Final can also be used with static private static final String name = "BlueEyeWhiteDragon"; //super keyword to call the parent class method public void testSuper() { super.attack(); } //This is the rewriting of the method, annotated with @ Override annotation @Override public String attack() { return "Explosive blast bomb of destruction"; } } public class LearningDemo { public static void main(String[] args)throws NullPointerException { // TODO Auto-generated method stub Monster m = new Monster(); System.out.println(m.attack()); //The embodiment of polymorphism. //Shape up: the parent class references the child class. Monsters can be blue eyed white dragons. Monster b1 = new BlueEyesWhiteDragon(); System.out.println(b1.attack()); //In the downward transformation, the blue eyed white dragon is also a monster, but pay attention to the strong transformation of type and instanceOf //BlueEyesWhiteDragon b2 = (BlueEyesWhiteDragon) new Monster(); This sentence will report an exception if(b1 instanceof Monster){ b1 = new Monster(); System.out.println(b1.attack()); }else { System.out.println("Your blue eyed white dragon has the wrong inheritance"); } } }
The modeling in the sample code is the embodiment of polymorphism. The downward transformation is usually the first upward modeling and then downward transformation.
1.8 single instance mode
What is singleton mode?
Singleton mode, also known as singleton mode, refers to a class in which only one instance exists in a JVM. It saves resources well. Applies to instances that need to be loaded only once.
Singleton patterns are: hungry and lazy. The similarities and differences between the two can be reflected in the following examples:
1.8.1 hungry Han single instance mode
Hungry Han mode code reflects:
package BasicDemo07_ClassAndObject; /** * This class is used to test * @author Sharry * For learning * */ public class HungryInstance { //Hungry Han style //Properties and methods of class private int age; public void hungry() { System.out.println("I'm a hungry instance"); } //The privatization of construction method is a feature of singleton pattern, which makes it impossible for external classes to directly new the objects of this class private HungryInstance() { } //Provide indirect methods to create objects through class attributes //Class attribute: in order to ensure that it is loaded only once and save resources, use static modification private static HungryInstance instance = new HungryInstance(); //The method that points to the property of this class public static HungryInstance getInstance() { return instance; } }
Test the hungry man mode above:
package BasicDemo07_ClassAndObject; /** * This class is used to test * @author Sharry * For learning * */ public class TestInstance { public static void main(String[] args) { // TODO Auto-generated method stub HungryInstance hungryInstance = HungryInstance.getInstance(); //Run once to check the effect hungryInstance.hungry(); //Multiple references point to this instance. Check the single instance effect: HungryInstance h1 = HungryInstance.getInstance(); HungryInstance h2 = HungryInstance.getInstance(); //The output hashCode is consistent System.out.println(h1); System.out.println(hungryInstance); //true System.out.println(h1==h2); } }
1.8.2 lazy singleton mode
The hungry man mode reflects the control of the hardware resources of the class that only needs to be loaded once. The lazy man mode controls the time resources, that is, create an instance for you when you use it, "lazier":
//Lazy style //Properties and methods of class private int age; public void lazy() { System.out.println("I'm a lazy instance"); } //Privatization of construction methods private LazyInstance() { } //Provide indirect methods to create objects through class attributes //Let it = null first, and then create an instance when necessary private static LazyInstance instance = null; //The method that points to the property of this class public static LazyInstance getInstance() { if(instance==null){ instance = new LazyInstance(); } return instance; }
1.7.3 three characteristics of singleton mode:
- Privatization of construction methods
- Static properties point to instances
- The getInstance method of public static returns the static properties of the second step
2, Enumeration
Enum is a special class, enum, which contains constants (constants are usually capitalized). It is often used with switch to determine the value range of case.
Go and learn more about enumeration when you have time!