day07 - object oriented Foundation

1, Object oriented

1. Relationship between class and object

  • What is a class
    • A collection of objects with the same properties and behavior
    • Class is an abstract and broad concept. For example: fruits, animals
    • Components of class:
      • Attribute (member variable): that is, the description information in the class
      • Behavior (member method): that is, what classes can do
  • What is an object
    • Object is the concrete embodiment of a certain kind of things
    • For example: Fruits - apples, animals - dogs
  • Relationship between classes and objects
    • Object is created according to class!

2. Definition class

  • Specify the name of the class

    • See the name and know the meaning
  • Specify which attributes are in the class

    • Properties - member variables
    • Data type variable name;
  • Specify what behavior exists in the class

    • Behavior member method
    • It is completely consistent with the previous method definition. Remove static
  • Sample code

public class Phone {
    //Member variable
    String brand;
    int price;

    //Member method
    public void call() {
        System.out.println("phone");
    }

    public void sendMessage() {
        System.out.println("send message");
    }
}

3. Create objects and use

  • Format for creating objects
    • Class name object name = new class name ();
  • How to use member variables
    • Object name Member variable name
  • How to use member methods
    • Object name Member method name ();
  • Sample code
public class PhoneDemo {
    public static void main(String[] args) {
        //create object
        Phone p = new Phone();

        //Using member variables
        System.out.println(p.brand);
        System.out.println(p.price);

        p.brand = "millet";
        p.price = 2999;

        System.out.println(p.brand);
        System.out.println(p.price);

        //Using member methods
        p.call();
        p.sendMessage();
    }
}

4. Definition and use of student class

  • Student class
public class Student {
    //Member variable
    String name;
    int age;

    //Member method
    public void study() {
        System.out.println("study hard and make progress every day");
    }

    public void doHomework() {
        System.out.println("The keyboard is broken, and the monthly salary is more than 10000");
    }
}
  • Test class
public class StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();

        //Use object
        System.out.println(s.name + "," + s.age);

        s.name = "Lin Qingxia";
        s.age = 30;

        System.out.println(s.name + "," + s.age);

        s.study();
        s.doHomework();
    }
}

2, Memory map of object

1. Memory diagram of a single object

2. Memory diagram of two objects

3. Multiple objects point to the same memory space

4. Differences between member variables and local variables

  • Define location differences
    • Member variable: defined outside the method in the class
    • Local variable: defined inside a method or on a method declaration
  • Default initialization value difference
    • Member variable: has default initialization value
    • Local variable: no default initialization value. Must be assigned before use
  • Memory location difference
    • Member variables: in heap memory
    • Local variables: in stack memory
  • Life cycle differences
    • Member variable: it is created with the creation of the object and disappears with the disappearance of the object
    • Local variable: it is created as the method is called and disappears as the method disappears

3, Encapsulation

1.private keyword

  • Role: permission modifier (private). Members decorated with private can only be accessed in this class

  • Sample code

    • Student class
    public class Student {
        //Member variable
        private String name;    // "Lin Qingxia"
        private int age;        // 30
    
        //get/set method
        public void setName(String n) { // String n = "Lin Qingxia"
            name = n;
        }
    
        public String getName() {
            return name;
        }
    
        public void setAge(int a) { // int a = 30
            age = a;
        }
    
        public int getAge() {
            return age;
        }
    
        public void show() {
            System.out.println(name + "," + age);
        }
    }
    
    • Test class
    public class StudentDemo {
        public static void main(String[] args) {
            //create object
            Student s = new Student();
    
            //Assign values to member variables using the set method
            s.setName("Lin Qingxia");
            s.setAge(30);
    
            s.show();
    
            //Use the get method to get the value of the member variable
            System.out.println(s.getName() + "---" + s.getAge());
            System.out.println(s.getName() + "," + s.getAge());
    
        }
    }
    

2.this keyword

  • effect

    • You can distinguish the problem of duplicate names of member variables and local variables! this modifies a member variable.
    • this represents the current object. I represent whoever calls me.
  • Sample code

    • Person class
    public class Person {
        //Member variable
        private String name;
        private int age;
    
        //get and set methods
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName(){
            return name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        //Member method
        public void show() {
            System.out.println("full name:" + name + ",Age:" + age);
        }
    }
    
    • Test class
    public class PersonTest {
        public static void main(String[] args) {
            //create object
            Person p1 = new Person();
            //Assign values to member variables
            p1.setName("Zhang San");
            p1.setAge(23);
    
            //Call the show method
            p1.show();
    
            System.out.println("-----------------");
    
            Person p2 = new Person();
            p2.setName("Li Si");
            p2.setAge(24);
    
            p2.show();
    
            //Gets the age of the p2 object
            int age = p2.getAge();
            System.out.println(age);
        }
    }
    

3. Packaging

  • Encapsulation idea: hide members who don't want to be exposed to the public. If the outside world wants to visit, provide public access!
  • Embodiment 1: private keyword. Improve code security!
  • Embodiment 2: method. Improve code reusability!

4, Construction method

1. What is the construction method

  • Construction methods are used to create objects. Initialize the members in the object! ps: (do constructors have any other functions besides creating objects? What's the difference between having constructors in a class and not having constructors?)

2. Construction method definition format

Permission modifier method name[Class name](){
    
}

3. Construction method and precautions

  • If no constructor is written, the system will provide an empty parameter constructor for us to use
  • If we write any constructor, the system will no longer provide the default null parameter constructor
  • Construction methods can also be overloaded

5, Definition and use of standard classes

1. Definition of class

  • Composition of classes
    • Member variables: all private modifiers
    • Construction method: null parameter and all parameter
    • get and set methods
    • Member method
  • Student class
public class Student {
    //Member variable
    private String name;
    private int age;

    //Construction method
    public Student() {

    }

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    //get and set methods
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    //Member method
    public void show() {
        System.out.println("The name is:" + name + ",Age is:" + age);
    }

    public void study() {
        System.out.println("Student learning");
    }
}

2. Use of class

  • Two ways to create objects
    • Null parameter construction + set method assignment
    • Create the object and assign the value ps with the parameter construction method: (does this have encapsulation effect? Which method has higher security than the get/set / method, or is it the same, why is it higher, and why is it the same?)
  • Test class
public class StudentTest {
    public static void main(String[] args) {
        //Null parameter construction method + set
        Student s1 = new Student();
        //Assign values to member variables
        s1.setName("Zhang San");
        s1.setAge(23);

        //Call member method
        s1.show();
        s1.study();

        System.out.println("------------------");

        //Parametric structure
        Student s2 = new Student("Li Si",24);

        //Call member method
        s2.show();
        s2.study();
    }
}

Exercises

Question 1: analyze and implement the following requirements

Mobile phone class Phone
 attribute:
	brand brand
	Price price
 behavior:
	phone call()
	send message sendMessage()
	play a game playGame()
	
requirement:
	1.Define classes according to the above requirements,Property to be private,Generate null and parametric structures, setter and getter method
	2.Define test class,stay main Method and assign a value to the property(Demonstrate two methods:setter Method and construction method)
	3.Call three member methods,The print format is as follows:
		I'm using Xiaomi's mobile phone with a price of 998 yuan....
		Is using the price of 998 yuan Xiaomi brand mobile phone to send text messages....
		We are playing games with Xiaomi brand mobile phones with a price of 998 yuan....

Question 2: analyze and implement the following requirements

1.Project Manager Manager 
	Properties:
		full name name
		Job number id
		wages salary
		bonus bonus
	Behavior:
		work work()
2.Programmer class Coder
	Properties:
		full name name
		Job number id
		wages salary
	Behavior:
		work work()
requirement:
	1.As defined above Manager Class and Coder class,Property to be private,Generate null and parametric structures, setter and getter method
	2.Define test class,stay main Method and assign a value to the property(Demonstrate two methods:setter Method and construction method)
	3.Call member method,The print format is as follows:
		The project manager with job No. 123, basic salary of 15000 and bonus of 6000 is trying to do management work,Assign tasks,Check the code submitted by the employee.....
		Programmers with job number 135 and basic salary 10000 are trying to write code......

Question 3: analyze and implement the following requirements

1.Cats Cat		
	attribute:
		Hair color color
		varieties breed
	behavior:
		having dinner eat()
		Catch a mouse catchMouse()
2.Dogs  Dog
	attribute:
		Hair color color
		varieties breed
	behavior:
		having dinner eat()
		Housekeeping lookHome()
requirement:
	1.As defined above Cat Class and Dog class,Property to be private,Generate null and parametric structures, setter and getter method
	2.Define test class,stay main Method and assign a value to the property(Demonstrate two methods:setter Method and construction method)
	3.Call member method,The print format is as follows:
		The colorful Persian cat is eating fish.....   
		The colorful Persian cat is catching mice....
		The black Tibetan mastiff is gnawing bones.....
		The black Tibetan mastiff is watching the house.....	

Concept questions

  • What is a class? What is an object? What is the relationship between classes and objects?

  • What is the difference between member variables and local variables?

  • What is the function of the private keyword?

  • What is the idea of encapsulation?

  • What is the definition format of the construction method?
    Age(23);

      //Call member method
      s1.show();
      s1.study();
    
      System.out.println("------------------");
    
      //Parametric structure
      Student s2 = new Student("Li Si",24);
    
      //Call member method
      s2.show();
      s2.study();
    

    }
    }

Keywords: Java

Added by AAFDesign on Sun, 02 Jan 2022 18:54:01 +0200