------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Object-oriented thinking is to see everything from the attributes, methods, constructors, code blocks, internal classes, such as five aspects of the composition, as long as things in these five areas have similarities can be classified as a class, between classes and classes is through encapsulation, inheritance, polymorphism to reflect.
First of all say encapsulation, very simple thought, God created us to come out without giving us wings, then we will not be able to fly even after the day after tomorrow through great efforts. This means that while each class is highly scalable, you cannot think of the future of children lying in the nursery as the great scientists who will lead humanity to the earth, and you do not know that they would be our next president, though slightly exaggerated, but these can be done through the day after tomorrow, It is impossible to grow a pair of wings. So if you're God, you're going to create a species. It is also necessary to set boundaries when providing him with the same = space for development. Through encapsulation can be solved, design personal class, in others fill his gender is we certainly hope he can only be "male" or "female", you can not make other options, also see why. In terms of jargon, it's about hiding things you don't want to tell others, and exposing them to others. It practices modifying access to properties to restrict access to properties, and creates a pair of values and assignments for each property for access to those properties.
Class Student{private String name;//private adornment, only this class can access private int age;private char gender;public void Setgender (char gender) { Set value for property if (gender== ' Man ' | | gender== ' female ') {This.gender=gender;} Else{system.out.println ("Please enter the correct gender");}} Public Char Getgender () {//provides a publicly accessible method of return this.gender;}} public class A6_12{public static void Main (string[] args) {Student one=new Student (); One.setgender (' F '); System.out.println (One.getgender ());}}
It is worth mentioning that private is only one of the means of encapsulation, as long as it is not accessible is called encapsulation.
Inherited:
Inheritance can effectively simplify the code, improve the reusability of code, but also because of the inheritance of the later polymorphic. The relationship between the two is bound by extends.
person{//parent class, also known as superclass, base class string Name;int age;} Class Student extends person{//inherits the properties of the parent class void study () {System.out.println ("learning");}} Class Teather extends person{//inherits the properties of the parent class void Teath () {System.out.println ("teach");}}
Subclasses have members of the parent class, including members decorated by public,default,final,static. In memory as the child class object is created, the parent class is also created, because the subclass implies a super ();
Super ();
So if the object accesses its members, is it in the parent class or in the subclass, then the two keywords can be solved:
this. member variable name super. Member Variable name
First, in the absence of keyword guidance, the system defaults to the "This" keyword in the sub-class to find, can not find again to find in the parent class, can not find the error. If you want to go through the subclass and search for the parent class directly, you can use the Super keyword.
The greatest implication of inheriting a subclass is that the method of overriding the parent class gets what it wants. There are two prerequisites before rewriting: 1. The method permission must be greater than or equal to the parent class permission Public>default>private 2. The method is the same, including whether there is a return value, the return value must be a subclass of the parent class return value, a parameter list, a method name, The exception that is thrown cannot be greater than the exception of the parent class. It is worth mentioning that static can only be overridden by static.
Class animal{string type;public void Run () {System.out.println ("Running");}} Class Cat extends animal{} class Dog extends animal{public void run (String a) {type= "rhubarb dog"; System.out.println (type+ "humming a song Running");}} public class A6_48{public static void Main (string[] args) {cat c=new cat (); C.run ();//Because there is no method for subclasses, the method of finding the parent class Dog D=new dog ();d. Run ();d. Run ("DD");}}
The child parent class can be transformed between the default upward transformation, that is, the child parent can be converted to a parent class, and if the parent class is converted to a subclass, a cast is required and the format is consistent with the base type cast. The coercion is that they want to have a relationship, by instenceof the keyword can be judged, return Boolean type, true there is an inheritance relationship, flase no inheritance relationship.
Anima instenceof Dog
Polymorphic:
From four aspects to explain polymorphism: 1. Polymorphism, the parent refers to the sub-class object;
2. The precondition of polymorphism, between the class and the class must be and inherit, but also have to rewrite;
3. Multi-state benefits, improve the class extension of the code;
4. Multiple days of malpractice, members in the parent class can only be accessed with references in the parent class.
For example, a Tetris random deformation is used polymorphic.
Import Java.util.random;class fk{//parent class void Bx () {//subclass necessary to override this method of the parent class System.out.println ("deform");}} Class FK_SZX extends fk{//type void bx () {System.out.println ("Mountain subtype in Metamorphosis");}} Class Fk_lzx extends FK {//l-shaped void bx () {System.out.println ("L-shaped in Warp");}} Class Fk_zzx extends fk{//z-shaped void bx () {System.out.println ("Z-font in Warp");}} Class FK_CFX extends fk{//rectangle void bx () {System.out.println ("rectangular variant");}} Class Fk_tzx extends Fk{void bx () {System.out.println ("field variant");}} Class A6_53{public static void Main (string[] args) {random r=new random (); int a=r.nextint (5); FK Fk=null;switch (a) {case 0:fk=new fk_szx (); break;case 1:fk=new fk_lzx (); break;case 2:fk=new fk_zzx (); Break;case 3:fk= New Fk_cfx (); break;case 4:fk=new fk_tzx (); break;} FK.BX ();//That is, the parent refers to the subclass object}}
It is worth mentioning that at the time of the program compiling is a member of the parent class, the runtime is referring to a member in a subclass, but a static member is a reference to the parent class that compiles and runs, because he already has the memory area loaded with the class.
Dark Horse programmer ———— The three main features of object-oriented in Java