Thought for a while, found that the individual package and polymorphism nothing to say, we will simply talk about the syntax in Java it.
The relevant contents are as follows:
One, access modifiers
Second, Getter/setter method
Third, the construction method
IV,Super and this
Five, thestatic keyword
Vi. Final Keywords
Vii. methods of rewriting
VIII. abstract classes and interfaces
One, access modifiers
There are four cases in Java, in the order of their access ranges from small to large, respectively:
priate (private)---->default (default)---->protected (Protected)---->public (public)
The scope of the visit can be seen:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/06/22/wKiom1myNxuQPLdKAAE5Kz-gujY567.png "title=" 11. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:99PX; "alt=" Wkiom1mynxuqpldkaae5kz-gujy567.png "/>
Second, Getter/setter method
This belongs to the package, the previous example of the goddess of age, the general age must not be less than 0, if less than 0 to give a default value of 18, so we use the Get/set method to encapsulate this property, you can define the value of the property.
private int age;//age, defined as private public int getage () {return;} public void Setage (int age) {if (age<=0) {this.age = 18;} Else{this.age = Age;}}
Third, the construction method
Construction methods are typically used to assign values to an object's properties when the object is initialized , before we create a code that writes the Goddess object:
Create three goddess Type objects Goddess Lin = new goddess ();//Goddess Object lin.name = "Chi Ling"; lin.age = 43;lin.stature = 1.73;lin.facevalue = 99;//Yan Value
If you use the constructor method, you need to write a method in which there is no return value type , and the method name is the same as the class name, and in order to ensure that you can create a blank object, we will also write an argument with the following code:
public class Goddess extends Woman {public int facevalue;//yan value public int getfacevalue () {return facevalue;} public void Setfacevalue (int facevalue) {this.facevalue = Facevalue;} Public goddess () {}//In order to ensure that blank objects can be initialized later, leave the construction method of no parameters public goddess (String Name,int, double stature, int facevalue) {This . SetName (name); This.setage (age); this.setstature (stature); This.setfacevalue (Facevalue);}}
The code for creating objects is much simpler:
Goddess Lin = new Goddess ("Chi Ling", 43, 1.73, 99);//Goddess Object
IV,Super and this
Observant people should find that the "This" keyword appears in the code above, which means "current object" and another "super" that represents the parent class object.
The above code can be modified as follows, if the parent class also has a constructor method, the subclass can use "super" to invoke:
The construction method code in the parent class is public class Woman () {public Woman (String name,int, double stature) {this.setname (name); This.setage (age ); This.setstature (stature);}} Construction method code in subclass public class Goddess extends Woman {public Goddess (String Name,int age, double stature, int facevalue) {super (n Ame, age, stature); This.setfacevalue (Facevalue);}}
Note: The constructor method of calling the parent class super () must be written in the first row.
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A4/D6/wKioL1myShXA9_RQAAQrgMywefs597.png "title=" 12. PNG "width=" 550 "height=" 235 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:550px;height:235px; "alt=" wkiol1myshxa9_ Rqaaqrgmywefs597.png "/>
Five, thestatic keyword
Something that is modified by static, which we call static. More commonly used are static variables, static methods, static blocks of code.
5.1 Static variables
Add a static variable in the Goddess class public static int count = 0;//Goddess Total//test code public static void Main (string[] args) {//Create three Goddess type Object Goddess Lin = New Goddess ("Lin Chi-ling", 43, 1.73, 99);//Goddess Object lin.count++; Goddess Wang = new goddess ();//Goddess Object Wang.count++;wang.setname ("condom"); Goddess Li = new goddess ();//Goddess Object Li.setname ("Bingbing"); li.count++; System.out.println (Lin.count);//result is 3}
The final print result is 3, which means that count is an attribute shared by the entire goddess community. Changing the value of the Count property of a is 250, then the value of the Count property for B and C also becomes 250.
5.2 Static Methods
Static methods, like the use of static variables, are also common to the entire population, and are defined as static, with the benefit of static methods that can be accessed directly from the class name without creating an object.
Add static method to Goddess class public static void Haha () {System.out.println ("I am the Goddess!") ");} Test code public static void Main (string[] args) {Goddess.haha (); Calling methods by class name}
5.3 Static code block
This thing is more fun, it only executes once when the JVM loads the class .
Static{system.out.println ("I am a static code block! ");}
Execution order of Java code:
Static variable initialization → static code block → initialize static method → Initialize instance variable → code block → construction method
Vi. Final Keywords
Can be used to modify properties, methods, and classes.
Placed before the property, indicating that the property is a constant, must be assigned at the time of definition, and cannot be modified later.
Before the method, it means that the method cannot be overridden by the quilt class (the next point will refer to the method override).
Put in front of the class, indicating that the class is not allowed to be inherited.
Code I will not demonstrate, this is relatively simple, the boys to try on their own OK.
Vii. methods of rewriting
Method overrides the target of the subclass to overwrite the implementation of a method of the parent class. Said straightforward point is Lao Tzu is a miner, there are mining methods, with shovels to dig mine, son is a miner, inherited the method of miners, but he is to use excavators to dig mine.
Miner Class (parent Class) public class Miner {public void Mining () {System.out.println ("Digging with spades one spade one spade!") ");}} Digger (subclass) public class Digmember extends Miner {//Overrides the mining method of the parent class public void mining () {System.out.println ("Dig Mine with excavator!") ");}} Test code public static void Main (string[] args) {Miner Miner = new Digmember ();//Create sub-class object of miner Miner.mining ();//Call mining method}
Results:
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A4/D7/wKioL1myTI_DVQinAAAa_9fMuE8175.png "title=" 13. PNG "alt=" Wkiol1myti_dvqinaaaa_9fmue8175.png "/>
The result can be found, although the parent class variable is defined , but new is a subclass object , so the method called is a subclass . If it is a parent object of new, then the call result is the parent class method.
VIII. abstract classes and interfaces
abstract class: An abstract class ..., its greatest feature is that it cannot be instantiated directly, it must first have a subclass (not an abstract class), and then it can be the new child class object. Change the miners to abstract classes:
Public abstract class Miner {public void Mining () {System.out.println ("Digging with spades one spade one spade!") ");}}
If you create a miner object now, you will report a syntax error:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A4/D7/wKioL1myTbWBZinUAAAdpAIIIRw707.png "title=" 14. PNG "alt=" Wkiol1mytbwbzinuaaadpaiiirw707.png "/>
However, it is possible to create subclasses ( subclasses cannot be abstract classes ) objects:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A4/D7/wKioL1myTe7RKYbOAAAgDMg4m5I785.png "title=" 15. PNG "alt=" Wkiol1myte7rkyboaaagdmg4m5i785.png "/>
Why use abstract classes? Because some classes are abstract Ah!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0025.gif "alt=" J_0025.gif "/>
For example, animals, what color are animals, please? A few legs? How did it move? How many wings are there?
Don't even know!!!
Do not know, because animals are an abstract concept, like what cats, dogs, people, the Japanese and so on is a subclass of animals. Can you find a primitive animal in real life that is neither a cat nor a dog, or is it a subclass of any other animal?
If you can, please accept my knees!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "alt=" J_0004.gif "/>
interface (interface): What is an interface? Take out your USB stick to see, and then plug into the computer's USB interface to try, if your mouse is USB interface, please look at the mouse interface and USB stick interface is there anything different?
Smart people should have found that the two interfaces are identical!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0036.gif "alt=" J_0036.gif "/>
Interface generally just defines a standard, it does not have a specific function (look at the USB interface on your computer, please tell me what it is?) Can you heat the food? Can you refrigerate your drinks? Can I see a movie? Not even! )。
But we can implement the standard interface tools, such as the implementation of the interface of the USB stick, you can use this interface to save data, the interface to implement the keyboard, you can enter data through this interface to the computer.
If the digger in the example above would drive, and the miner would not, and there would be another separate driver class to drive, then we could define the drive as an interface. Need to drive the class, to implement this interface on the line.
Defining the Driving interface//driving interface public interface IDrive {//driving method void Drive ();} Mining class implementation drive interface public class Digmember extends Miner implements IDrive {public void Mining () {System.out.println ("Dig Mine with excavator!") ");} public void Drive () {//TODO auto-generated method StubSystem.out.println ("I'll open the excavator!!!" ");}}
Finally: What is the difference between an abstract class and an interface?
Abstract classes focus on one group, while interfaces focus on functionality.
Everybody realizes the following case to know the difference:
Create a collection (or an array) Save all runners (rabbits, turtles, chicks, lions) and create a collection (or array) to hold all the players (airplanes, Eagles, Superman, Optimus Prime) in the flight race. Note that these players will fly, but not necessarily the same parent class.
Today's content is here, all only said some basic grammar, there are a lot of use of the details, we can take some examples of the project, we together to analyze ha!
"Software Thinking" blog address: 51CTO , Blog Park , interested small partners can go to see the relevant other blog posts.
This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1963751
"Java from getting started to giving up" javase: Object-oriented Syntax II (introductory version)