[Detailed + super basic] Java-study notes 02

Object-oriented

Java language is an object-oriented programming language, and object-oriented thinking is a kind of programming thinking. Under the guidance of object-oriented thinking, we use Java language to design and develop computer programs. The object here refers to all things in reality, and each thing has its own attributes and behaviors. Object-oriented thinking is to refer to things in reality in the process of computer programming, abstract the attributes and behaviors of things, and describe them as computer events. It is different from the process-oriented thinking, which emphasizes the realization of functions by calling the behavior of the object, rather than the operation and realization step by step.

Example for washing clothes: process-oriented: take the clothes off -> find a basin -> put some washing powder -> add some water -> soak for 10 minutes -> knead -> wash clothes -> wring dry -> air it and face the object : Take off the clothes -> turn on the fully automatic washing machine -> throw the clothes -> button -> hang up

One, class and object

1.1 Understanding of classes and objects

Class understanding

Class is an abstraction of things that have common attributes and behaviors in real life

A class is the data type of an object, and a class is a collection of objects with the same properties and behavior

Simple understanding: Class is a description of real things

Class composition

Attribute: refers to the characteristics of things, such as: mobile phone things (brand, price, size)

Behavior: refers to the operations that things can perform, such as mobile phone things (calling, texting)

The relationship between class and object

Class: Class is an abstraction of things that have common attributes and behaviors in real life

Object: It is a real entity that can be seen and touched. Objects that exist objectively are all objects, so we often say that everything is an object.

Simple understanding: a class is a description of things, and an object is a concrete thing

1.2 Definition of class [application]

The composition of the class is composed of two parts: attributes and behaviors

Attribute: reflected in the class through member variables (variables outside the method in the class)

Behavior: Reflected in the class through member methods (compared with the previous method, remove the static keyword)

Class definition steps: ①define the class ②write the member variables of the class ③write the member methods of the class

Sample code:

public class 类名 {

// 成员变量

变量1的数据类型 变量1;

变量2的数据类型 变量2; …

// 成员方法

方法1; 方法2;}

public class Phone {

//成员变量

String brand;

int price;

//成员方法

public void call() { System.out.println("打电话"); }

public void sendMessage() { System.out.println("发短信"); }

}

1.3 Use of the object [application]

The format of the created object:

Class name object name = new class name();

The format of calling members:

Object name. Member variable

Object name. member method();

Sample code

/* 创建对象

格式:类名 对象名 = new 类名();

范例:Phone p = new Phone();

使用对象

1:使用成员变量

格式:对象名.变量名 范例:p.brand

2:使用成员方法 格式:对象名.方法名() 范例:p.call()

*/ public class PhoneDemo { public static void main(String[] args) {

//创建对象 Phone p = new Phone();

//使用成员变量

p.brand = "小米"; p.price = 2999;

System.out.println(p.brand);

System.out.println(p.price);

//使用成员方法

p.call(); p.sendMessage(); } }

1.4 Student target-exercise [application]

Requirements: First define a student class, and then define a student test class, in the student test class to complete the use of member variables and member methods through objects

Analysis: Member variables: name, age...

Member method: study, do homework...

Sample code:

class Student {

//成员变量

String name;

int age;

//成员方法

public void study() { System.out.println("好好学习,天天向上"); }

public void doHomework() { System.out.println("键盘敲烂,月薪过万"); }

}

/* 学生测试类 */

public class StudentDemo { public static void main(String[] args) {

//创建对象 Student s = new Student();

//使用对象

s.name = "孙不坚";

s.age = 18;

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

s.study();

s.doHomework();

} }

1.5 Notes

Guide package: point out where the classes need to be used.

import package name. class name;

import demo01.Student;

In the case of belonging to the same package as the current class, you can omit the package-leading statement.

Member variables and local variables

Different positions in the class: member variables (outside the method in the class) local variables (inside the method or on the method declaration)

Different locations in memory: member variables (heap memory), local variables (stack memory)

The scope is different: member variables (in the entire class) local variables (in the method)

The life cycle is different: member variables (exist with the existence of the object, disappear with the disappearance of the object) local variables (exist with the call of the method, and disappear after the call of the drunk method)

Different initialization values: member variables (with default initialization values) local variables (without default initialization values, you must first define them before you can use them)

Second, the object memory diagram

(To be sorted out)

Three characteristics of object-oriented:

Encapsulation, inheritance, polymorphism

One, package

The manifestation of encapsulation in java:

1. The method is a kind of packaging

2. The keyword private is also a kind of encapsulation

1.1 private keyword [understand]

Private is a modifier that can be used to modify members (member variables, member methods)

Members modified by private can only be accessed in this class. For member variables modified by private, corresponding operations are provided if they need to be used by other classes

Provide "get variable name()" method to get the value of member variable, the method is decorated with public

Provide "set variable name (parameter)" method, used to set the value of member variables, the method is decorated with public

Sample code:

/* 学生类 */

class Student {

//成员变量

String name;

private int age;

//提供get/set方法

public void setAge(int a)

{ if(a<0 || a>120) { System.out.println("你输入的年龄有误"); }

else {

age = a; } }

public int getAge() {

return age; }

//成员方法

public void show() {

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

/* 学生测试类 */

public class StudentDemo {

public static void main(String[] args) {

//创建对象 Student s = new Student();

//给成员变量赋值 s.name = "孙不坚";

s.setAge(18);

//调用show方法

s.show(); } }

1.2 Use of private [application]

Requirements: Define a standard student class, require name and age to be decorated with private, and provide set and get methods and a show method that is convenient for displaying data. Objects are created and used in the test class, and the final console output Sun Bujian 18

Sample code:

/* 学生类 */

class Student {

//成员变量

private String name;

private int age;

//get/set方法

public void setName(String n) {

name = n; }

public String getName() {

return name; }

public void setAge(int a) {

age = a; }

public int getAge() {

return age; }

public void show() { System.out.println(name + "," + age); } }

/* 学生测试类 */

public class StudentDemo {

public static void main(String[] args) {

//创建对象 Student s = new Student();

//使用set方法给成员变量赋值

s.setName("孙不坚");

s.setAge(18);

s.show();

//使用get方法获取成员变量的值

System.out.println(s.getName() + "," + s.getAge()); } }

1.3 this keyword [application]

Variables modified by this are used to refer to member variables, and its main function is to distinguish between local variables and member variables with the same name.

If the formal parameter of the method has the same name as the member variable, the variable without this modification refers to the formal parameter, and the variable with this modification refers to the member variable

public class Student {

private String name;

private int age;

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; }

public void show() {

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

1.4 Packaging Ideas [Understanding]

  1. Encapsulation overview is one of the three major characteristics of object-oriented (encapsulation, inheritance, polymorphism). It is an object-oriented programming language that simulates the objective world. In the objective world, member variables are hidden inside the object and cannot be directly manipulated by the outside world.
  2. The encapsulation principle hides some information of the class inside the class and does not allow direct access by external programs. Instead, the method provided by the class is used to implement the operation of the hidden information and access the member variable private, and provide the corresponding getXxx()/setXxx( )method
  3. The benefits of encapsulation control the operation of member variables through methods, which improves the security of the code. Encapsulates the code with methods, which improves the reusability of the code

Two, construction method

1.1 Overview of the construction method

Construction method is a special method

Role: Create the object Student stu = new Student();

Format: public class class name {

Modifier class name (parameter) {

}

}

Function: mainly complete the initialization of object data

class Student {

private String name;

private int age;

//构造方法 public Student() {

System.out.println("无参构造方法"); }

public void show() {

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

}

/* 测试类 */

public class StudentDemo {

public static void main(String[] args) {

//创建对象

Student s = new Student();

s.show(); }

}

1.2 Precautions for the construction method [understanding]

Construction method creation

If no construction method is defined, the system will give a default no-parameter construction method. If a construction method is defined, the system will no longer provide a default construction method. The recommended method of use, no matter whether it is used or not, manually write a parameterless construction method

Overloading of construction methods

If you have customized a parameterized construction method, but also use a parameterless construction method, you must write another parameterless construction method.

Important function: You can use parameterized construction to initialize member variables

Sample code

class Student {

private String name;

private int age;

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

public void show() {

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

}

/* 测试类 */

public class StudentDemo {

public static void main(String[] args) {

//创建对象

Student s1 = new Student();

s1.show();

//public Student(String name)

Student s2 = new Student("孙不坚");

s2.show();

//public Student(int age)

Student s3 = new Student(18);

s3.show();

//public Student(String name,int age)

Student s4 = new Student("孙不坚",18);

s4.show(); } }

1.3 Standard production [application]

Requirement: Define the standard student class, which requires the use of empty parameter and parameter construction methods to create objects. Objects created with empty parameters are assigned through setXxx, objects created with parameters are assigned directly, and data is displayed through the show method.

Sample code:

class Student {

//成员变量

private String name;

private int age;

//构造方法

public Student() { }

public Student(String name, int age) {

this.name = name; this.age = age; }

//成员方法

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; }

public void show() {

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

/* 创建对象并为其成员变量赋值的两种方式

1:无参构造方法创建对象后使用setXxx()赋值

2:使用带参构造方法直接创建带有属性值的对象

*/

public class StudentDemo {

public static void main(String[] args) {

//无参构造方法创建对象后使用setXxx()赋值

Student s1 = new Student();

s1.setName("孙不坚");

s1.setAge(18);

s1.show();

//使用带参构造方法直接创建带有属性值的对象

Student s2 =new Student ("孙不坚",18 ) ;

s2.show();

}

}

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/112974046