Java object-oriented (foundation): one

1. Object-oriented thinking

Object-oriented is the core content of the java language. Many beginners are confused when they are new to object-oriented. What is a class? What is an object? What is their relationship like? Wait for the questions to come up, today I will do my own object-oriented learning and sharing for everyone.
Before learning object-oriented, I will show you a picture:

Insert picture description hereObserving this picture, it is not difficult to see: the first block diagram is very specific at every step from getting up to going to class and riding in the car; while the second block diagram, We only need to put the clothes in the washing machine, and then take the clothes out after washing. Then we can regard the first block diagram as "process-oriented", and our second block diagram is our protagonist today "object-oriented".

So what is the difference between the two?
Process-oriented thinking: We are participants in every step of doing a thing, focusing on the problem-solving process.
Object-oriented thinking: only focus on the final result.

2. Object-oriented features

1. More in line with the way humans think about problems
2. Change us from the executor of the event to the caller
3. Simplify the problem

3. Classes and objects

Next we will introduce classes and objects.
Class : A class of things with the same attributes and behaviors is called a class; a class is abstract, invisible and intangible.
Object : Everything is an object, visible and tangible.
The relationship between a class and an object : an object is a specific description of a class, and a class is an abstraction of an object.
Let me give you an example: a
Insert picture description here
student is a class, and the object is a specific student.
If I say now: Students come out, will anyone agree?
But if I say: student Zhang San comes out, then Zhang San will know that he is calling him.

Attribute: Describe the information that the thing has.
Behavior: What the thing can do.
Note: In java, class is the most basic unit, so we have to learn to describe things with class.
Things → class
attributes → member variables
behavior → member methods

4. Object-oriented development design and characteristics

Development : Constantly create objects, use objects, modify object
design : manage and maintain the relationship between objects. A good object design can simplify the program and be easy to maintain.
Features : encapsulation, inheritance, polymorphism

5. Example: Put the elephant in the refrigerator

Next, let us use object-oriented thinking to analyze:
1. What are the classes?
Category: Elephant, Refrigerator, OOPDemo
2. What behaviors (methods) are there in each category
Elephant: Eating grass, walking
Refrigerator:
Opening and closing doors OOPDemo: Methods of loading elephants main
3. Classes and classes What is the relationship between the source code display: Elephant class:

Insert picture description here
Refrigerator category:
Insert picture description here
OOPDemo:
Insert picture description here
Here, I used two methods, object-oriented and process-oriented, to facilitate your understanding.
The results are as follows:Insert picture description here

As you can see, process-oriented and object-oriented produce the same results, so someone will ask, why is object-oriented even if they produce the same result?
Let me put it this way: Now we are just pretending to be elephants, so if we are going to be pretending to be monkeys, puppies, tigers, etc..., are we going to print it over and over again? We use object-oriented thinking to set the class in advance, and when it is used, no matter what it is installed, it can be directly called, wouldn't it be more convenient!

6. Use of classes

6.1 Class definition:
Insert picture description here

//我们创建一个学生类
public class Student{
    
    
//成员变量
String name
int age
//构造方法
//成员方法
}

6.2 call of class:
Insert picture description here

Student tom = new Student();//实例化语法
Student tom = new Student("tom",20);
//调用类中的属性:
tom.name
tom.age
//调用类中的方法:
tom.getAge(20);

6.3 Construction method:
Insert picture description here

public Student(){
    
    
//无参构造方法
}

//构造有参方法
 public Student(String name, int age){
    
    
//this后续我会讲解,大家先不用注意
    this.name = name;
    this.age = age;


}

Note:
1. The name and type of the constructor must be consistent
. 2. There is no visitor
. 3. If a parameterized construction method is defined in a class, the invisible non-parameter construction will not exist. At this time, it is recommended to display the definition of the non-parameter construction method.
4. Each class has a construction method. If the construction method is not explicitly defined for the class, the Java compiler will provide a default implicit non-parameter construction method for the class. Method
5. When creating an object of a class, at least one construction method should be invoked.
6. There can be multiple construction methods in a class, and they constitute overloads, that is, construction method overloads.

7. The usage of this

this : Represents the object
usage of the current class :
1. Used to distinguish between member variables and local variables with the same name.
2. You can call the construction method of the current class. Syntax: this([parameter list]); call the no-parameter construction method of the current class without passing parameters, on the contrary, call the parameter construction method that matches the parameter list. Note: This call statement must be the first statement in the method body.
3. Can call member attribute: this. Member attribute name
4. Can call member method: this. Member method name ([parameter list]);
code demonstration of this :

//this关键字的应用:
//1.调用当前类的属性(区分成员变量和局部变量同名赋值问题)
//2.调用当前类的方法
//3.调用当前类的其他构造方法
//
//
//注意:this关键字访问类的成员方法时不受访问权限的控制,可以访问本类中所有的成员变量和成员方法,包括private 的成员变量和成员方法
public class Person
String name;
int age;
//显示无参构造方法
public Person(){
    
    
this("tom",20);
}
//定义有参构造
public Person(String name){
    
    
this.name = name;
public Person(String name,int age){
    
    
this(name);
this.age = age;
}
public void  setName(String name){
    
    
this.name = name;
}
public void testMethod(String name){
    
    
this.setName(name);//通过this调用了当前类中的其他方法
System.out.println(this.name);
}
public static void main(String[] args) {
    
    
Person person = new Person();

System.out.println("___________this调用本类属性________________");
person.setName("tom");
System.out.println("name1"+person.name);
System.out.println("___________this调用本类方法________________");
person.testMethod("lili");
System.out.println("___________this调用本类构造方法_____________");
Person person1 = new Person("tom",30);
System.out.println(person.name);
System.out.println(person.age);
}
}

8. Class application code:

Next, I will show you the application code of the class. We define a student class, define a student test class, and then call our properties and methods from the student class!

//我们定义一个学生类
Public class Student{
    
    
String name;
int age;
String email;
//显示定义无参方法
public Student(){
    
    
}
//定义构造有参方法
public Student(String name ,int age, String email){
    
    
this.name = name;
this.age = age;
this.email = email;
}
//成员方法
//获取姓名
public String getName(){
    
    
  return name;
}
public void setName(){
    
    
this.name = name;
}
public int getAge(int age){
    
    
if(age>0 &&age<18){
    
    
this.age = 18;
}else{
    
    
this.age = age;
}
return this.age;
}
}
//学生类的测试类
//需求:调用学生类中的成员属性和方法
public class StudentTest{
    
    
public static void main(String[]  args ){
    
    
//实例化Student中的对象
Student tom = new Student();
//调用属性:对象名.属性名
tom.name = "tom ford";
tom.age = 20;
tom.email = "[email protected]"
System.out.println("姓名:"+tom.name);
System.out.println("年龄:"+tom.age);
System.out.println("邮箱:"+tom.email);

System.out.println("_________________________________________________________");
//如果上述Student中属性过多,则这种方法很慢,很麻烦。
//通过有参构造一次性构造
Student tom = new student("tom",20,"[email protected]");
System.out.println("姓名:"+tom.name);
System.out.println("年龄:"+tom.age);
System.out.println("邮箱:"+tom.email);


System.out.println("_________________________________________________________");

//调用方法:对象名.方法名()
int tomAge = tom.getAge(20);
System.out.println("tomAge:"+tomAge);
System.out.println("姓名:"+tom.getName());
tom.name =("dd");
System.out.println("name:"+tom.getName());

}


}

9. There can be the following variables in a class

Class variable : exists in the class, outside the method, static keyword must be added before the declaration: such as: static int flag; generated with the loading of the class;
member variable:
local variable:

the difference between local variable and member variable:
1 . The location of declaration is different.
Member variables (instance variables): in the class, outside the method.
Local variables: in the method body or on the method declaration.
2. The storage locations in the memory are different.
Member variables: in the heap memory.
Local variables: in the inner layer of the stack
3. The life cycle is different.
Member variables: produced as the object is produced, and die with the death of the object.
Local variables: produced as the method is called, and end when the method is called.
4. Different initial state
member variables: Yes Default initialization value
Local variable: There is no default initialization value, it can be used after manual initialization.
5. Execution priority
When the member variable and the local variable have the same name, the "principle of proximity" is adopted when calling;

Guess you like

Origin blog.csdn.net/zjdzka/article/details/109455091