The article directories

  • Classes and objects
    • 1. Look at a cat problem
    • 2. Use existing technologies
    • 3. Analysis of shortcomings solved by existing technologies
    • 4. A program is a world with many things (objects [properties, actions]).
    • 5. Schematic diagram of the relationship between classes and objects
    • 6. Quick Start – Object oriented solution to cat problems
    • 7. Differences and relationships between classes and objects
    • 8. Objects have form in memory (important)
    • 9. Attributes/member variables/fields
    • 10. Notes and details of attributes
    • 11. How do I create objects
    • 12. How do I access properties
    • 13. Memory allocation mechanisms for classes and objects (critical)
    • 14. Object mechanism exercises

Classes and objects

1. Look at a cat problem

  • Mrs. Zhang has two cats: a three-year-old white cat named Xiao Bai. There is also a little flower, 100 years old this year, color. Please write a program, when the user enters the name of the cat, it shows the cat’s name, age, color. If the user enters the wrong name of the cat, it will show that Mrs. Zhang does not have the cat.

2. Use existing technologies

Object01.java

  1. Define variables separately
  2. Using arrays

3. Analysis of shortcomings solved by existing technologies

  • Bad for data management
  • Low efficiency === “leads to our new knowledge points and object philosophy, Taoist thought
  • Java designers introduced classes and objects (OOP) at the root because existing technologies could not perfectly address new and new requirements.
  • code
	// Separate variables to solve => bad data management (you take a cat's information apart)
	
	// First cat information
	
	String cat1Name = "White";
	int cat1Age = 3;
	String cat1Color = "White";
	
	// The second cat
	String cat2Name = "Flower";
	int cat2Age = 100;
	String cat2Color = "Design and color";
Copy the code
  • Use an array
	// Array ===>(1) Data type is not reflected
			//(2) only [subscript] can be used to obtain information, resulting in the corresponding relationship between variable name and content is not clear
			//(3) do not reflect the cat's behavior
	
	String[] cat1 = {"White"."3"."White"}; 
	String[] cat2 = {"Flower"."100"."Design and color"};
Copy the code

4. A program is a world with many things (objects [properties, actions]).

5. Schematic diagram of the relationship between classes and objects

6. Quick Start – Object oriented solution to cat problems

  • Using Object Orientation
	// Define a Cat -> custom datatype
	class Cat {
	// Attributes/member variables
	String name; / / name
	int age; / / age
	String color; / / color
	double weight; / / weight
	/ / behavior
	}
	
	// Use OOP object-oriented solution
	// Create a cat object
	/ / read
	//1. new Cat() creates a Cat(Cat object)
	//2. Cat cat1 = new Cat(); Assign the created cat to cat1
	//3. Cat1 is an object
	Cat cat1 = new Cat();
	cat1.name = "White";
	cat1.age = 3;
	cat1.color = "White";
	cat1.weight = 10;
	// Create a second cat and assign it to cat2
	// Cat2 is also an object (cat object)
	Cat cat2 = new Cat();
	cat2.name = "Flower";
	cat2.age = 100;
	cat2.color = "Design and color";
	cat2.weight = 20;
	
	// How to access the properties of the object
	System.out.println("The first cat message." + cat1.name 
		+ "" + cat1.age + "" + cat1.color + "" + cat1.weight);
	
	System.out.println("The second cat message." + cat2.name 
		+ "" + cat2.age + "" + cat2.color + "" + cat2.weight);
Copy the code

7. Differences and relationships between classes and objects

From the above cases and explanations, we can see:

  1. Classes are abstract, conceptual, representing a class of things, like humans, cats… That is, it is a data type.
  2. An object is concrete, actual, representing a concrete thing, i.e. an instance.
  3. A class is a template for an object, and an object is an individual of the class, corresponding to an instance

8. Objects have form in memory (important)

9. Attributes/member variables/fields

  • Basic introduction
  1. As in concept or designation:Member variable = property = field(Field)(That is, member variables are used to represent attributes, collectively called attributes)
  • Case demonstration:Car(name,price,color)

    Object02.java
class Car{
    String name;// Attribute, member variable, field
    double price;
    String color;
    String[] master;// Attributes can be primitive data types or reference types (objects, arrays)
}
Copy the code
  1. An attribute is a component of a class, typically a primitive data type, but also a reference type (object, array). Like the one that defined catsint ageIs the property

10. Notes and details of attributes

PropertiesDetail.java

  1. Property definition syntax same as variable, example: access modifier Property type Property name;

    Here’s a brief introduction to access modifiers:Controls the access scope of attributes

    There are four types of access modifiersPublic proctected default private
  2. The definition type of an attribute can be any type, including a base type or a reference type
  3. Properties have default values if they are not assigned, the same rules as arrays. Specifically:
int 0.short 0.byte 0.long 0.float 0.0.double 0.0.char\ u0000,boolean falseThe Stringnull
Copy the code
  • Example: [Person class]
public class PropertiesDetail { 

	// Write a main method
	public static void main(String[] args) {
		// Create Person object
		//p1 is the object name (object reference)
		// The object space (data) created by new Person() is the real object
		Person p1 = new Person();

		// The default value for the property of the object, following the array rules:
		//int 0, short 0, byte 0, long 0, float 0.0,double 0.0, char \u0000, Boolean false, String null;
	
		System.out.println("\n Information about the current person");
		System.out.println("age=" + p1.age + " name=" 
				+ p1.name + " sal=" + p1.sal + " isPass="+ p1.isPass) ; }}class Person {
	// Four attributes
	int age;
	String name;
	double sal;
	boolean isPass;
}
Copy the code

11. How do I create objects

  1. Declare before you create
Cat cat ; // Declare the object cat
cat = new Cat(); / / create
Copy the code
  1. Directly to create
Cat cat = new Cat();
Copy the code

12. How do I access properties

  • The basic grammar
Object name. Attribute name.Copy the code

Examples demonstrate assignment and output

cat.name;
cat.age;
cat.color;
Copy the code
  • Memory allocation mechanisms for classes and objects (important)

Look at a thought question

  • Define a human being (Person)(including name, age). (Object03.java)



	public class Object03 { 
	
		// Write a main method
		public static void main(String[] args) {
	
			Person p1 = new Person();
			p1.age = 10;
			p1.name = "Xiao Ming";
			Person p2 = p1; // assign p1 to P2 so that P2 points to P1
			System.out.println(p2.age); / / 10}}class Person {
		String name;
		int age; 
	}
Copy the code

13. Memory allocation mechanisms for classes and objects (critical)

  • Structure analysis of Java memory
  1. Heap: Generally holds basic data types (local variables)
  2. Stack: Holds objects (Cat Cat, arrays, reference data types, etc.)
  3. Method area: constant pool (constants, such as strings), class loading information
  4. Schematic diagram [Cat (name, age, price)]



  • Java object creation process simple analysis

14. Object mechanism exercises

  • Look at an exercise and analyze the memory layout



  • Code:
	public class Object04 {
	    public static void main(String[] args) {
	        Person02 a = new Person02();
	        a.name = "Xiao Ming";
	        a.age = 10;
	        Person02 b;
	        b = a;
	        System.out.println(b.name);
	        b.age = 200;
	        b = null; System.out.println(a.age); System.out.println(b.age); }}class Person02{
	    String name;
	    int age;
	}
Copy the code