[JAVA] 8. Classes and Objects

Keywords: Attribute Java Mobile

Object-Oriented (OPP)

  • Object-oriented is an idea
  • Process Oriented: Emphasizing Process, Functional Behavior (Executor)
  • Object-Oriented: Encapsulate functions into objects, emphasizing functional objects (commanders)
  • Three characteristics: encapsulation, inheritance, polymorphism
  • Development: It's about finding objects to use. Without an object, create an object and encapsulate the functionality
  • Features: Finding objects, building objects, using objects, maintaining the relationship between objects

EXAMPLE: I'm driving to Xinjiang.

· Procedure-oriented: I want to drive, I pull the door, I sit in the driver's seat, I have been in Shandong Province.
(Take charge of the process)

· Object-Oriented: I ordered the car to go to Xinjiang (car. Go). How the car did not care about the past, do not need to understand the whole driving process, the car exposed function is where to go.
(The car encapsulates its own information and functions within the car class)

Class and Object Relations

  • Class: Description of things in real life. (Descriptor: Name, age, gender, etc.), mapped to Java, is the class defined.
  • Class is a set of objects with the same attributes and methods, describing the specific information of the object. In a class, the attributes and methods of the object are defined.
  • Participants:
    • Each object has a type, which is an instance of a class.
    • Each object belongs to a specific "class", which has its own general characteristics and behavior. It's this kind of thing, a real individual (such as a person), that corresponds to the entity created by JAVA in heap memory with new.

Example 1: Staff (Category)

Attributes (Static, Specific Values): Name, Age, Wage Amount - Membership Variables
Method (What can the object do, function): Display name (), age (), and salary ()

Different objects can be derived from classes -- instantiation
Staff A: Name: A, age: 20, salary: 4000 - object (encapsulation of member variables and methods)

public class Text {
    public static void main(String[] args){
        Worker a = new Worker();//a is the Worker class type variable - object
        a.name = "A";
        a.age = 20;
        a.wages = 4000;
        a.showname();
        a.showage();
        a.getwages();   
    }
}
//Describe the staff (class)
class Worker {
    //Membership variables (attributes), what are the objects
    String name;
    int age;
    int wages;
    //Method (function). What can an object do?
    void showname() {
        System.out.println("Full name:"+name);
    }
    void showage(){
        System.out.println("Age:"+age);
    }
    void getwages() {
        System.out.println("Wage amount:"+wages);
    }
}
//Operation results:
//Name: A
//Age:20
//Wage amount:4000

Example 2: People open the door - analysis: (looking for nouns)

Human {
    Open the door (door){
        Door. Open.
    }
}
Door {
    Open () {
    Operating door shafts, etc.
    ...
    }
}
// The most appropriate method should appear in the most appropriate class
 // How to open a door is the clearest in itself, so opening this function should be defined in the door.

3. Object-Oriented Design Ideas

1. For a problem, the first thing to consider is which classes and objects should be included.
2. Then consider what attributes and methods each class and object should have.
3. Consider again what kind of relationship exists between classes and classes.

IV. Definition of Classes

1. All JAVA programs are organized by class. [Class is the keyword for defining classes]
2. Composition of classes: member variables and methods
3. Steps to define classes
· Define class names
· Writing class attributes
· Method of writing classes

Class class name{
    // What are the attributes that define classes?
    Type attribute 1 of attribute 1;
    Type attribute 2 of attribute 2;
    ...
    // What can a method of definition do?
    Method 1;
    Method 2;
    ...
}

Example: To buy a mobile phone, first of all, you need to think about what kind of mobile phone you want, and the imaginary mobile phone is the class. Screen size, CPU, memory, etc. are represented by attributes, phone calls, text messages and other functions.

//1. Define a class
public class Telephone {
    //2. Declare attributes. What are they?
    float screen;//Screen size
    float cpu;
    float mem;//Memory size
    //3. Define the method and what to do
    void call() {
        System.out.println("Telephone function");
    }
    void sendMessage() {
        System.out.println("Short Message Function");
    }
}

V. Users

  • Create Objects: Class Name Object Name = new Class Name (); // Located in heap memory, dynamically allocating memory
Telephone phone = new Telephone();
  • Use Object: Object Name. Attribute / Method
phone.mem = 4.0f;//Assign 4, float type to mem attribute
phone.call();//callcallMethod
  • Note: Each object of the same class has different member variable storage space, and each object of the same class shares the method of this class.

6. Local variables and member variables

1, definition

  • Local variables (in stack memory) are defined in methods, where data is temporarily saved
  • Membership variables (in heap memory) are defined in classes because objects exist in memory.

2. Scope

  • The role of local variables in defined methods or statements
  • The role of a member variable in the entire class that defines it
  • No one knows what's going on in the declaration.

3. Initial value

  • JAVA gives member variables an initial value, not a local variable.
  • Initial values of member variables:

4. In one method, local variables with the same name are not allowed. In different methods, local variables with the same name are allowed.

5. When two kinds of variables are identical in name, local variables have higher priority.

public class a {
    int a = 1;
    void b() {
        int a = 2;
        System.out.println(a);
    }
}
//Operation results: 2

VII. Anonymous Objects

public class Text {
    public static void main(String[] args){
        //Worker a = new Worker(); named object
        //a.name = "A";
        //Anonymous object, no object name defined
         new Worker().name = "A";
         //When this sentence is executed and A is given a name, it becomes garbage.
         new Worker().age = 18;
         //In heap memory, another area is generated, and when it is executed, the memory becomes garbage again.
         //So it's meaningless for anonymous objects to call attributes
         new Worker().showname();
         //Anonymous object invocation method makes sense
    }
}
class Worker {
    String name;
    int age;
    int wages;
    void showname() {
        System.out.println("Full name:"+name);
    }
}
//If multiple member calls are made to an object, the object must be named.

//When anonymous objects are used:
//1. When the method of the object is called only once.
//2. Anonymous objects can be passed as actual parameters
public class Text {
    public static void main(String[] args){
        //Worker a = new Worker(); named object
        //show(a);
        show(new Worker());//After executing the heap memory area, it becomes garbage.
        //new Worker() passed to a as an argument is equivalent to Worker a = new Worker()
    }
    public static void show(Worker a) {
        a.name = "A";
        a.showname();
    }
}
class Worker {
    String name;
    void showname() {
        System.out.println("Full name:"+name);
    }
}

Posted by roldahayes on Sun, 24 Mar 2019 19:42:29 -0700