"Java Learning note 13" On Java object-oriented process and code implementation

Source: Internet
Author: User

Understand important Java object-oriented knowledge points:

First, class, object

    class? First, give an example: Xiao Li designed a car design, and then handed over to the production plant to produce cars, there are black, red, white ... Here, the car design is what we call the class, the production shop is the new constructor (most of the objects are new), the production of the car is what we want to say the object. It can be said that the essence of Java programming is the process of building classes.

    object? All things are objects, in the universe, such as plants, animals, humans, each individual is a his ability. This requires that the object be high cohesion, low-coupling (simple understanding is the human brain, which is responsible for thinking, imagining, remembering, not breathing, detoxification, digestion of food, thus ensuring its independence and efficiency). Object has two things State (property) and behavior (methods), see the following code:

Student.java

1  Public classStudent {2String name;//name3     intAge//Age4String Classno;//class5String Hobby;//Hobbies6     //Output Information Method7      Public voidShow () {8SYSTEM.OUT.PRINTLN (name + "\ n:" + age + "\ n"): "+9Classno + "\ n Hobby:" +hobby);Ten     } One}

Initialstudent.java

1  Public classinitialstudent {2      Public Static voidMain (String args[]) {3Student Student =NewStudent ();//Build Objects4Student.name = "Zhang Hao";//Assigning a value to an object5Student.age = 10;6Student.classno = "S1 class"; 7Student.hobby = "Basketball";8Student.show ();//Calling Methods9} }

Ii. encapsulation, inheritance, polymorphism

    package? to give a vulgar analogy, my mobile hard disk saved a lot of various types of information, but roommates often borrow, from time to time to take my use (formatted?) Installation system? Save a movie? This will give me the owner of a very large inconvenience, I told him that this hard disk is my private, you have to follow my permission to use the way! The same is true for objects that do not arbitrarily access internal data, which can cause "cross-infection." So we need to encapsulate: privatize properties, provide public access to private properties.

Adult.java

1  Public classAdult {2     Private intAge ;3      Public intGetage () {4         returnAge ;5     }6      Public voidSetage (intAge ) {7         if(Age < 18) {8SYSTEM.OUT.PRINTLN ("Error! The minimum age should be 18 years old! ");9              This. age = 18;//If the age requirement is not met, the default value is givenTen         } One         Else{ A          This. Age =Age ; -         } -     } the  -      PublicString toString () { -         return"[age=" + Age + "]"; -     } +}

Testadult.java

1  Public classTestadult {2      Public Static voidMain (string[] args) {3Adult P1 =Newadult (); 4P1.setage (20);//Private property access to the adult class through Seter, Geter, ToString5System.out.println (P1.tostring ());//increased data access restrictions to ensure maintainability6     }7}

inheritance? the manager felt that Xiao Li's design of the car was great, but now he is advocating new energy, so he needs to change the engine into an electric engine. So Xiao Chen easy to the drawings in the gasoline engine part of the engine engine, and then re-make a relationship to complete the new drawing! This is the function of inheritance: To create a new class using the definition of an existing class, the new class can have the characteristics of the parent class, or it can derive more features. This makes it easy to reuse the previous code, which can significantly shorten development time.

Person.java

1 classperson {//Define Human2      PublicString Mname;//name3      Public intMAge;//Age4 5      Public voidDining () {6System.out.println ("Full of food ...");7}//the way to eat8 }9 Ten classStudentextendsperson {//the student class inherits from the human One      Public floatMgrade;//Achievements A  -      Public voidexamination () { -SYSTEM.OUT.PRINTLN ("Pass the exam ..."); the}//How to test - } -  - classTeacherextendsperson {//Teacher class inherits from Human +      Public floatMsalary;//Salary -  +      Public voidprelection () { ASystem.out.println ("Very tired in class ..."); at}//the way of class -}

Testperson.java

1  Public classTestperson {2      Public Static voidMain (string[] args) {3Student std =NewStudent ();//Instantiating student Objects4Std.mname = "Li Dong";5Std.mage = 18;//assign a value to a name and age, access a member in the parent class6Std.dining ();//call the method of eating, access the members of the parent class7Std.examination ();//call the test method, access the members of the subclass8 9Teacher Teacher =NewTeacher ();//instantiate a Teacher objectTenTeacher.mname = "Zhao Zhongxiang"; OneTeacher.mage = 72;//assign a value to a name and age, access a member in the parent class ATeacher.dining ();//call the method of eating, access the members of the parent class -Teacher.prelection ();//call the test method, access the members of the subclass -     } the}

Polymorphic ? the specific type of reference variable defined in the program and the method call made through the reference variable are not deterministic during programming, but are determined during the program's run, that is, the instance object of the class to which the reference variable is inverted, and the method call that the reference variable emits is the method that is implemented in the class. Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software. Xiao Li likes to listen to birds singing {Sparrow, Cuckoo, parrot}

Xiao Li: The bird outside the window, sing me a song.

1. (Bird bird = new Sparrow)?

2. (Bird bird = new cuckoo)?

3. (Bird bird = new Parrot)?

Bird: bird.sing () ~~~~~

Xiao Li: Birds sing well, what kind of bird are you?

Bird: Bird.shape ()

Xiao Li: (---If the blue character above defines 3, is a parrot) haha! Oh, you're a parrot!

So, the process of polymorphism is essentially an abstract instruction that allows a group of individuals with the same behavior to work together with different content in a single process.

"Java Learning note 13" On Java object-oriented process and code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.