Java Object orientation

First, object-oriented thought

1. Baidu Baike’s explanation

Baidu Baike’s explanation of “object-oriented thinking” :

Object-oriented programming languages must have language components that describe objects and their relationships to each other. These programming languages can be grouped into the following categories: everything in a system is an object; An object is an encapsulation of a property and its operations; Objects can be divided into classes according to their properties and become instances of classes. Instance relation and inheritance relation are static relation between objects. Messaging is the only form of dynamic association between objects, and the only form of computation; The method is a sequence of messages.

2. Use diagrams to understand “object-oriented thinking”

3. What are the common objects in your life?

Therefore, object-oriented thinking is a way of thinking, not a specific technology.

To implement such ideas into Java programming, there are several technical points involved: objects, classes, inheritance, interfaces, abstraction, encapsulation, and polymorphism.

Classes and objects

Class: is a template that describes the behavior and state of an object.

Object: is an instance of a class.

For example, a person is a class, and a concrete person (e.g., Zhang SAN, Xiao LAN) is an object.

1. Class example – “People”

Software objects also have states and behaviors. The states of software objects are attributes, and behaviors are reflected through methods.

2. Classes — A way to describe “humans” in UML

3. Write “people” programs in Chinese

Next, slowly move the above “software design language” into the Java program, first to the Chinese version of the description:

Source code: com.chapter01.testClass.people

package com.chapter01.testclass; 
/** * define "person" */
public class{
     // Some attributes
    privateString ID number;privateString name;private charGender ='male';
    private shortAge =0;
    privateString native place;/** * eat */
    public voidTo eat () {if('male'== gender) {system.out.println (thisThe name +"Yes"+thisGender +."Yes, eat two bowls of it!");
        }else {
            System.out.println (thisThe name +"Yes"+thisGender +."Yeah, eat half a bowl of rice, lose weight..."); }}/** ** sleep *@paramBedtime *@paramWake-up time *@returnSleep status */
    publicThe String bed (intBedtime,intWake up time) {intSleep time = wake up time - sleep time; String Sleep status =""; 
        
        if(Sleep time >8) {Sleep status ="Enough sleep, sleep ("+ sleep time +") hours."; 
        }else{sleep status ="Not enough sleep, just sleep ("+ sleep time +") hours."; 
        }returnSleep condition; }/ / test
    public static void main(String[] args) {person first1Personal =newPeople ();// Fill the instance of "first person" with the property value of the first person.set id number ("50024543543567"); The first1Personal name ("Zhang"); The first1Individual.'male'); The first1Set age ((short) 19); The first1Native place ("Guangzhou, Guangdong province");
        // The first person's eating behavior1Personal meals ();// The first person's sleeping behaviorString sleep result = first1Personal. Sleep (9.15); System. Out.println (the first1Get name ()+ sleep result); System.out.println("============ Make another man out ============"); People first2Personal =newPeople ();// Fill in the property values for the instance "first person"2Personal id number ("50024519891209245677"); The first2Personal name ("Xiao LAN"); The first2Individual.'woman'); The first2Set age ((short) 25); The first2Native place ("Chongqing Jiangbei District"); 
        // The first person's eating behavior2Personal meals ();// The first person's sleeping behaviorString result = first2Personal. Sleep (9.18); System. Out.println (the first2Get name ()+ result; }// The following get/set methods are generated by IDEA.
    publicString get ID () {returnId number; }public voidSet ID number (String ID number) {thisId Number = ID Number; }publicString get name () {returnThe name; }public voidSet name (String name) {this.name = name; }public charGet gender () {returnSex; }public voidThe set of gender,charGender) {this. Sex = sex; }public shortGet age () {returnAge; }public voidThe set age (shortAge) {this.age = age; }publicString get native place () {returnNative place; }public voidSet Native place (String Native place) {thisNative place = native place; }}Copy the code

The above running results are as follows:

Zhang SAN is a man, to eat more than two bowls! Joe didn't get enough sleep, he just slept (6) hours = = = = = = = = = = = = reengineering a person out = = = = = = = = = = = = xiaolan is female of, only eat half bowl of rice, lose weight... Xiao LAN has had enough sleep and has gone to sleep.9Process finished with exit code0
Copy the code

In principle, there is no problem to describe things in Chinese, Java language can also compile, code readability is still very high, but the expression, beauty, format, specification, internationalization of the program is not as good as English, so, generally use English letters to write programs.

4. Class – Write “human” programs in English

Here is the English version:

Source: com. Chapter01. Testclass PersonClass

package com.chapter01.testclass;
 /** * define "PersonClass" */
  
  public class PersonClass {
    // Some attributes
    private String idCard; 
    private String name; 
    private char sex = 'male'; 
    private short age = 0; 
    private String nativePlace; 
    /** * eat */
    public void eat(a) {
        if('male'== sex){
            System.out.println (this.name +"Yes"+this.sex +"Yes, eat two bowls of it!"); 
        }else {
            System.out.println (this.name +"Yes"+this.sex +"Yeah, eat half a bowl of rice, lose weight..."); }}/**
     *sleep 
     * @param startSleepTime 
     * @param getUpTime 
     * @returnSleep status */
    public String sleep(int startSleepTime, int getUpTime) {
        int totalSleepTime = getUpTime-startSleepTime;
        String sleepState = ""; 
        if(totalSleepTime>8) {
            sleepState = "Enough sleep, sleep ("+totalSleepTime+") hours."; 
        }else { 
            sleepState = "Not enough sleep, just sleep ("+totalSleepTime+") hours."; 
        }return sleepState; 
    }
    // Other behavior
    //TODO 
    public static void main(String[] args) { 
        PersonClass personClass = new PersonClass ();

        // Fill in the property values for the "first person" instance
        personClass.setIdCard ("50024543543567"); 
        personClass.setName ("Zhang"); 
        personClass.setSex ('male'); 
        personClass.setAge ((short) 19); 
        personClass.setNativePlace ("Guangzhou, Guangdong province"); 
        // The first person's eating behavior
        personClass.eat (); 
        // The first person's sleeping behavior
        String rs01 = personClass.sleep (9.15); 
        System.out.println (personClass.getName ()+rs01); 
        System.out.println("============ Make another man out ============"); 
        PersonClass personClass02 = new PersonClass (); 
        // Populate the "second person" instance with property values
        personClass02.setIdCard ("50024519891209245677"); 
        personClass02.setName ("Xiao LAN"); 
        personClass02.setSex ('woman'); 
        personClass02.setAge ((short) 25); 
        personClass02.setNativePlace ("Chongqing Jiangbei District"); 
        // The eating behavior of the second person
        personClass02.eat (); 
        // The second person's sleeping behavior
        String rs02 = personClass02.sleep (9.18); 
        System.out.println (personClass02.getName ()+rs02); 
    }
    
    public String getIdCard(a) {
        return idCard;
    }
    public void setIdCard(String idCard) { 
        this.idCard = idCard; 
    }
    public String getName(a) { 
        return name; 
    }
    public void setName(String name) { 
        this.name = name; 
    }
    public char getSex(a) { 
        return sex; 
    }
    public void setSex(char sex) { 
        this.sex = sex; 
    }
    public short getAge(a) { 
        return age; 
    }
    public void setAge(short age) { 
        this.age = age; 
    }
    public String getNativePlace(a) { 
        return nativePlace; 
    }
    public void setNativePlace(String nativePlace) { 
        this.nativePlace = nativePlace; }}Copy the code

So adjust from Chinese to English do, this class basically became, the original program is so simple!

5. Object Example – Object (Zhang 3) :

PersonClass personClass = new PersonClass ();
 // Fill in the property values for the "first person" instance
personClass.setIdCard ("50024543543567");
personClass.setName ("Zhang"); 
personClass.setSex ('male'); 
personClass.setAge ((short) 19);
 personClass.setNativePlace ("Guangzhou, Guangdong province"); 
 // The first person's eating behavior
 personClass.eat (); 
 // The first person's sleeping behavior
 String rs01 = personClass.sleep (9.15); 
 System.out.println (personClass.getName ()+rs01);
Copy the code

6. Object – Create the second object of “Human” (Xiaolan) :

Create the second object of “Human” (Xiaolan) code:

PersonClass personClass02 = new PersonClass ();

// Populate the "second person" instance with property values
personClass02.setIdCard ("50024519891209245677");
personClass02.setName ("Xiao LAN");
personClass02.setSex ('woman');
personClass02.setAge ((short) 25);
personClass02.setNativePlace ("Chongqing Jiangbei District");

// The eating behavior of the second person
personClass02.eat ();
// The second person's sleeping behavior
String rs02 = personClass02.sleep (9.18);

System.out.println (personClass02.getName ()+rs02);
Copy the code