Can you learn java object-oriented? You'll see it after reading it

1. What is object-oriented

As we all know, java is an object-oriented programming language, so what is object-oriented? We often confuse object-oriented and process-oriented. Here is an example. It will be in a second!
For example, Xiao Ming has some dirty clothes to wash, which is equivalent to an order to wash them
A: Process oriented: Xiao Ming needs to complete some operations independently, such as fetching water, putting detergent, rubbing clothes, etc. until the clothes are cleaned
B: Object oriented: Xiao Ming doesn't need to finish it by himself, but he needs to send his clothes to a place with the function of washing clothes, such as the laundry. You need to apply for a membership card, and then say to the staff: I don't care what method you use, just wash it for me

2. Object oriented format

First, you need a class. There is no main method written in it. There will be three parts in it
A: Class variable B: class method C: constructor
Suppose there is a student class now

  • A: Class variable, also known as member variable, is used to describe the attributes of this class. A student must have some column attributes such as name, student number, age, gender and so on! So these become member variables
  • B: Class method: class method is the class. What functions does it have and what can it do? For example, a student can count, test, eat and other skills
  • C: Constructor: the constructor is to find an entity for this class. Now we only know that this is a student class and who the student class is. At present, we don't know. We need a constructor to define an entity. Note that there are two kinds of constructors: one is parameterless construction, and the other is parameterless construction. Constructors can have multiple or none, Next, let's talk about the difference between the two
    Nonparametric Construction: when a nonparametric construction is used to define an entity, it only indicates that there is a student, but the student's information is not said! When there is no constructor written in the student class, java will automatically provide a parameterless construct for this class to create objects (entities)
    Parametric Construction: for a student defined by a parametric construction, he also specifies some information about the student, such as the student's name and how old he is this year. The following code is used to explain the description

Students:

public class 	Student {
	
	//Member variable
String name;//Student name
String  sex;//Student gender
int age;//Student age

/**
 * Member method
 * Format: decorated value keyword return value type method name (parameter) {}
 * Modifier Keywords: public (for all places), protected (for the same project), not written (for the same package), private (for the same class)
 *Return value type: the result returned after completing this method
 *Parameters: parameters here refer to formal parameters	
 *
 */
public void eat() {
	System.out.println("I can eat");
}
public int add(int a,int b) {
	return a+b;
}
//Constructor, just remember this format
//Parametric structure
public Student(String name, String sex, int age) {
	super();
	this.name = name;
	this.sex = sex;
	this.age = age;
}
//Nonparametric structure
public Student() {	
}
}

Test class:
Because we don't write the main function in the student class, we can't run it. We need to write a test class

public class TestStudent {
	public static void main(String[] args) {
		//Create an object. How does the object come from? new comes out
		//This is a student, represented by stu1, but the student does not specify any information, so it is called nonparametric construction
		Student stu1=new Student();
		//Because stu1 is a Student, he can complete the functions in the Student class, entity object. Method name (); Can call
		stu1.eat();
		//This is a student, represented by stu2, but the student specifies his name, gender and age, which we call parametric structure
		Student stu2=new Student("zhangsan","nan",18);
	}
}

Note: a class can have no constructor or multiple constructors

3. Method overload

Method overloading is also important. Method overloading means that two methods in a class have the same name but different parameters. The code is as follows

public class Test {

	public void shouMe(String name,int age) {
		System.out.println("My name"+"name"+"I this year"+age);
	}public void shouMe(String name ,String sex,int age) {
		System.out.println("My name"+name+"My gender"+sex+"My age"+age);
	}
	public static void main(String[] args) {
		Test t1=new Test();
		t1.shouMe("zhangsan", 14);
		t1.shouMe("lisi", "nan", 15);
		/**
		 * Print results
		 * My name is name. I'm 14 this year
		*My name is lisi, my gender is nan, my age is 15
		 */
	}
}

4. Use of this

When the member variable in a class has the same name as the formal parameter of the method, the value of the member variable in the method needs to be used with this. Member variable

public class Stu{
String name="Zhang San";
public static void main(String[] args) {
Stu s1=new Stu();
s1.test("Li Si");
}
public void test(String name){
//The name here is a formal parameter
System.out.println(name);//The result is Li Si
System.out.println(this.name);//The result is Zhang San
}
}

5. static

Static is a commonly used keyword. Static often modifies variables, methods and static code blocks
Modified with static has two features: A: static B: sharing
B: Sharing: variables and methods modified with static indicate that they are not unique to an object, but are common to all objects. Variables and methods modified with static can be called directly by class name, method name / variable name without creating an object

public class Stu{
String name="Zhang San";
public static void main(String[] args) {

Stu.test("Li Si");//Because the test method is static, all can be used directly without creating objects, and so can variables
}
public static void test(String name){
//The name here is a formal parameter
System.out.println(name);//The result is Li Si
System.out.println(this.name);//The result is Zhang San
}
}

Static code block is the part that is executed when the class is loaded. It is the first one to execute, but it is only executed once. When the class is accessed again, the static code block will not be executed. Next, describe the static code block and constructor, and construct the code block execution order

public class sta {
/**
 
 * 
 * The static code block runs when the class is loaded. As long as the class file is loaded, the static code block will be executed only once
 * In a class, the static code block is executed first and run only once. The code block will be executed when the new object is constructed and run before the method is constructed
 * 
 * @param args
 */
	String name;
	int age;
	{
		//Construct code block
		System.out.println("I'm building blocks");
	}
	static {
		//Static code block
		System.out.println("I'm a static code block");
	}
	public sta() {
		//Non tragic constructor
		System.out.println("I am no tragedy");
	}
	
	public sta(String name, int age) {
//Parameterized constructor
		System.out.println("I have a parameter structure");
		this.name = name;
		this.age = age;
	}
	public static void main(String[] args) {
		sta s=new sta();
		sta s2=new sta("xzc", 21);
	}
	

}
result
 I'm a static code block
 I'm building blocks
 I am no tragedy
 I'm building blocks
 I have a parameter structure

So the execution order is static code block > construction code block > constructor
There is inheritance, which is one of the characteristics of java. It will be updated next time

Keywords: Java Back-end

Added by neo777ph on Fri, 29 Oct 2021 08:45:29 +0300