java classes and objects

Class: An abstraction and template for entities

One, the composition of the class

1. Properties (member variables)

2, methods (member methods, functions)

3. Constructor (role: create an object of the class, initialize the member variables of the object)

4. Code block (function:)

Create a class with class{ }

 
 

We created the class Person, which has its own properties, which is the sign of Person ----> name age sex... Then it has its own method ------> eat() sleep() .....

class Person{
	public String name;
	public int age;
	public String sex;
	
	public void eat(){
		System.out.println("Person : eat!!");
	}
	
	public void sleep(){
		System.out.println("Person : sleep!!");
	}
	
	public void show(){
		System.out.println("name :"+name+" age :"+age+" sex :"+sex);
	}
}

Next we create an object of the Person class

Object creation:

         1. Allocate memory for the object

         2. Call the appropriate constructor (more than one constructor)

Person person=new Person();//Instantiate an object
//person is a reference variable pointing to the reference address of the Person class

If there is no explicit writing of a constructor, java will call the system default constructor when running this sentence

public Person(){
}

Invocation of methods and properties in a class

person.eat();
person.sleep();

Called by object name.method name      

Two, four methods of object initialization

1. Through the method of object name.attribute (ps: At this time, the attribute should be modified by public)

Person person = new Person();
person.name="jiejie";
person.age=19;
person.sex="女";
person.show();

2. If the property in the class is private (private modification)

Can be initialized through a series of set() get() methods

       public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
person.setName("fairy");
person.setAge(10000);
person.setSex("女");
person.show();

Initialization result


3. Initialize the object through the parameterized constructor

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

If only this constructor is written, then executing the original sentence to create an object will report an error


Because there is an explicit constructor, the default constructor will not be called

		Person person = new Person("fairy1",999,"女");
		person.show();

Initialization result


! ! ! The constructor can have

Person person = new Person("fairy1",999,"女");
		Person person1=new Person();
		Person person2=new Person("fariy2",888);
		person.show();
		person1.show();
		person2.show();

public Person(String name,int age,String sex){
		this.name=name;//this represents the memory pointing to my current object because an object has not been successfully created when the constructor is called
		this.age=age;
		this.sex=sex;
	}
	public Person(){
		
	}
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}

The initialization result is


We can also call other constructors inside the constructor

public Person(){//Call the constructor with three construction parameters This sentence must be the first line
		this("fariy3",222,"女");
	}

operation result


4. Initialize the class through the code block

    1> Static code block

private static int data;//Static member variables are in the method area
private static final int data1 = 29;



Initialization result


  2> Example code block



If the constructor is masked out

The initialization result is


so:::


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483872&siteId=291194637