Refactoring the basics of Java (4) | Java object-oriented you care about

Process-oriented

Java is an object-oriented language. In an object-oriented system, a class is a collection of data and methods to manipulate data. Data and methods together describe the state and behavior of an object. Each object is an encapsulation of its state and behavior. Classes are arranged in a certain system and hierarchy so that subclasses can inherit behavior from superclasses. There is a root class in this class hierarchy, which is a class with general behavior. Java programs are organized by classes.

Object-oriented

OOP (Object Oriented Programming) object-oriented programming. Object-oriented programming is essentially to model real-world objects.

class

We abstract some things with the same properties and behaviors into one category.
A class is a model, which determines the characteristics (attributes) and behaviors (methods) that the object will have
:

  • Class is the type of object
  • A collection of objects with the same properties and methods

Cat:
attributes: color, age, weight,
behavior: sleep, catch mice, eat

Car category:
attributes: color, horsepower, four wheels,
behavior: driving, parking

在java语言中万物皆对象(在java里面不是所有的东西都是对象,八种基本数据类型不是对象,jdk1.5以后对于八种基本数据类型都提供了包装类)。

Object

A class is a concept, and an object is a concrete instance.
“有什么”
Attributes: various static characteristics of the
“能做什么”
object Method: various dynamic behaviors of the object

The relationship between class and object

  • Class is an abstract concept, just a template
  • The object is a concrete entity that you can see and touch
  • Class is the type of object
  • Object is a specific type of data
  • In the specific development process, first define the class and then instantiate the object

Class definition

grammar:

class 类名{
    
    
		属性
		方法
	}

注意: If it is the case of a class, it is best to let 类名and 文件名consistent.
There can only be 属性and. 行为
Class name: To see the meaning of the name, use English words, and capitalize the first character of each English word.
Insert picture description here

Insert picture description here

Attributes

The variable defined in the class is the attribute. The attribute can have no initial value, but the jvm will give the attribute a default value. The default value of the 只有属性才有默认值
basic data type is as follows:

Basic data type Defaults
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
char ‘ ’
boolean false

引用数据类型的默认值:null

method

Methods: Various dynamic behaviors of objects
Syntax:

public <static> 返回值  方法名字(数据类型 变量, 数据类型 变量……){
    
    
	<return>
}

Insert picture description here

Class instantiation

The purpose of creating a class is to create an object (an instance of the class)
syntax:

数据类型 变量 = new 数据类型();

Examples:

Insert picture description here
The above example creates a cat class

Access the properties of the object:

对象.属性名

Method of calling class:

对象.方法(行为)名

Examples:
Insert picture description here

The structure of the memory of the created object

When we run the class, it will generate jvm in memory. Jvm
has栈,堆,数据共享区

  • 栈:When the program is running, the location where the references of the basic data types and reference data types are stored, the memory is small, and the access speed is fast
  • 堆:It is the location where the object is stored, the memory is large, and the access speed is slow
  • 数据共享区:Memory area for storing shared data

Insert picture description here
Data sharing area:

Dog.class
lookHome()
sleep()

//类中的对象方法是被当前类的所有对象共享的。

Local variable

All variables defined in methods or statement blocks are local variables.

As follows:
Insert picture description here
the characteristics of local variables:

  1. Scope: limited to the defined braces.
  2. Storage location: stored in the stack memory.
  3. Default value: None, local variables must be assigned values.
  4. Life cycle: disappear at the end of the bracket
class Demo1{
    
    
	
	public static void main(String[] args){
    
    
		//局部的变量只在它的所属的大括号和其子孙括号内有效
		int x = 0;
		if(x > 10){
    
    
			System.out.println(x);
		}
	}
	
	public static void add(){
    
    
		//定义一个变量
		int i = 0;
		{
    
    
			int x = 0;
		}
		//报错的
		System.out.println(x);
		
	}
	//方法的参数这个变量的作用域就是这个方法的体内
	public static void add(int a){
    
    
		
	}
	
	public static void multiply(){
    
    
		//循环的变量的定义的也是在循环体的大括号内有效
        for(int i=0;i<10;i++){
    
    
        
        }
	}	
}

注意:Not only basic data types are local variables

Local variables must be initialized before use, otherwise an error will be reported

Insert picture description here
Error:
Insert picture description here
the life cycle of local variables
Insert picture description here

Class encapsulation

Object-oriented three characteristics: 封装, 继承, 多态
Objective: To improve data security, through the package, may be implemented to control access to the property, while increasing the maintainability.

Encapsulation

Encapsulation steps

  1. Private property
  2. Use set/get assignment and use this to reference
  3. Call method (method public)

Examples:

// 对System.out.println();的封装。
public class Test
{
    
    
  public static void test(String str)
  {
    
    
  System.out.println(str);
  }
}
 
  调用
  public class Use
  {
    
    
     public static void main(String[] args)
     {
    
    
      Test.test("封装");
     }
   }

Inheritance

If a class is allowed to inherit only one parent class, it is called single inheritance; if it is allowed to inherit multiple parent classes, it is called multiple inheritance. Java language interface of multiple inheritance by
example:

Class A{
    
    
Public  void  A1(){
    
    
		System.out.println(“我是特立独行的鱼儿”);
  }
}
Class B extends A{
    
    
	
}

Polymorphism

在这里插入代码片public Class Parents {
    
     
  public void print() {
    
    
    System.out.println(“parents”);
  }
}
public Class Father extends Parents {
    
     
  public void print() {
    
    
    System.out.println(“father”); 
  }
}
public Class Mother extends Parents {
    
    
  public void print() {
    
    
    System.out.println(“mother”);
  } 
}
  
public Class Test {
    
    
  public void find(Parents p) {
    
     
    p.print();
}
public static void main(String[] args) {
    
    
  Test t = new Test();
  Father f = new Father();
  Mother m = new Mother();
  t.find(f);
  t.find(m);
  }
}

This keyword

There is a this keyword hidden in the object method. If the object calls this method, the this in this method is the object

class Person{
    
    
	
	String name;
	int age;
	
	
	public void info(){
    
    
	
	/*对象方法中隐藏了一个this关键字, 如果那个对象调用了这个方法那么这个方法里面的this就是这个对象
*/
		System.out.println(this);
		
	}
}

public class TestPerson{
    
    
	
	public static void main(String[] args){
    
    
		//创建一个Person对象
		Person p = new Person();
		
		System.out.println(p);
		//调用info
		p.info();
			
	}	
}

This is used in the encapsulation to distinguish variables and attributes with the same name. The attribute with this is an
example:

class Person{
    
    
	
	private String name;
	private int age;
	
	public void setName(String name){
    
    
		//this.name是属性
		 this.name = name;
	}
	
	public String getName(){
    
    
		return name;
	}
	
	public void setAge(int age){
    
    
		this.age = age;
	}
	
	public int  getAge(){
    
    
		return age;
	}
}

Constructor

The constructor itself is a method:

  • There is no return value, and there is no void.
  • The method name of the constructor must be the same as the class name
  • Define the parameter list to be initialized in the method.
    Function: create an object and assign an initial value to the object;

Default constructor

public Person(){
    
    
   System.out.println("我是被鱼儿调用的构造器");
}

The constructor of a class has no parameters. This is the default constructor. If you don't write it, it will exist by default. The method body has nothing, and we can define it explicitly.

Parameterized constructor

Examples:

public Person(String name, int age){
    
    
		//给属性赋值
		this.name = name;
		this.age = age;
	}

Use a parameterized constructor to create an object:

Person p1 = new Person("特立独行的鱼儿", 20);
		p1.info();

If there is a constructor with parameters in a class, the default constructor without parameters will be overwritten and cannot be used. If you want to use the default constructor, you must define it explicitly.

Direct constructor call

public Person(String name, int age){
    
    
		this.name = name;
		this.age = age;
	}
	
	
	public Person(String name, int age, String gender){
    
    
	
		//通过this的用法调用本类的构造器
	
		/*
		this.name = name;
		this.age = age;
		*/
		this(name, age);
		this.gender = gender;
	}

To call the constructor of this class through this (variable, variable...), it must be placed in the first line of the call.

Singleton mode

When we used to create objects, it was possible to create many objects through a class, but we wanted to create only one object for a class.
Lazy man mode
example:

class Singleton{
    
    
	
	private static Singleton s;
	
	//给构造器私有化
	private Singleton(){
    
    
		
	}
	
	//私有化构造器之后,肯定不能被外部使用
	
	public static Singleton getInstance(){
    
    
		if(s == null){
    
    
			s = new Singleton();
		}
		return s;
	}
}


class TestSingleton{
    
    
	
	public static void main(String[] args){
    
    
		Singleton s = Singleton.getInstance();
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		System.out.println(s);
		System.out.println(s1);
		System.out.println(s2);
	}
}

Guess you like

Origin blog.csdn.net/weixin_43853746/article/details/107786465