Basic knowledge of classes and objects, constructors, method overloading and inheritance

(The following is my understanding of classes and objects, as well as some other basic knowledge of Java, welcome to point out any improprieties)

1. The high-level language is used to help us talk to the computer, and we guide the computer to complete functions through programming

2. Object-oriented analysis:

 1) Class and object

    Class: refers to a collection with many similarities, not entities

The method of creating a new class in eclipse:


<span style="color:#000000;">public class 类名</span>
<span style="color:#000000;">public class Teacher</span>

           Object: refers to the individuals that make up a class, which is an entity

The method of creating a new class is:

<span style="color:#000000;">类名 对象名=new 类名();</span>
<span style="font-size:14px;color:#000000;">Teacher t=new Teacher();</span>

2) Object-oriented programming analysis

Object-oriented is to focus only on who is the executor of the action (method)

For example: students attend class, this matter focuses on students rather than how students attend class


 3) Process-oriented programming analysis

Process-oriented means focusing on how actions (methods) are executed

The method of defining the characteristics of a class:

public class 属性名;
<span style="font-size:14px;color:#000000;">public String name;</span>

The method of the definition of a class method:

public 返回类型 方法名(参数列表)
<pre class="html" name="code">public void teach(String stuname)
	{
		System.out.println(name+"老师正在给"+stuname+"学生上课");
	}
 
 

The format of calling attributes with objects: object name.attribute name

Teacher t=new Teacher();
		//给对象命名
		t.name="张三";

The format of calling a method with an object: object name. method name (parameter)

Teacher t=new Teacher();
	        //执行行为(调用方法)
		t.teach("李四");


 
 Construction method: construct an object

Such as

Teacher t=new Teacher();

t is an object constructed

The in-memory stack roughly means the following figure:

The newly created object is passed to the object name and stored in the heap, and the attributes and methods of the class are stored in the stack

 

The basic format of the construction method: public class name (parameter list) {}

public void teach(String stuname)
	{
		System.out.println(name+"老师正在给"+stuname+"学生上课");
	}


Method overloading:

That is, in the same class, the method names of several methods are the same, but the parameters are different

  Such as the following

public class Teacher
{
public void teach(String stuname)
	{
		System.out.println(name+"老师正在给"+stuname+"学生上课");
	}
public void teach(String stuname)
	{
		System.out.println(...........);
	}
public void teach(String stuname,int stuage)
	{
		System.out.println(.......);
	}
public void teach(int stuage)
	{
		System.out.println(..........);
	}


}


The three methods are overloaded with each other


Inheritance:

The subclass inherits all the properties of the parent class except private properties, and can derive its own properties or methods

Some of the parent class, in addition to private attributes, subclasses have

Such as the following two categories:

public class A
{
	public String name;
	public void eat(){
		System.out.println("吃.......");
	}
}
public class B extends A
{
	B b=new B();
	b.eat();
}
B inherits the method of the parent class A through the keyword extends and calls it


Method rewrite:

The subclass overrides the method of the parent class by overriding the method inherited from the parent class, but the method name, parameters, return type, etc. cannot be changed

Such as the following example:

public class A
{
	public String name;
	public void eat(){
		System.out.println("吃.......");
	}
}
public class B extends A
{
	B b=new B();
	public void eat(){
		System.out.println("吃fffffffffffffff");
	}
	b.eat();
}


Then the final output should be "eat ffffffffffffffffffff" to
achieve method rewriting, which should be distinguished from overloading


Automatic transformation:

When the subclass rewrites the parent class method, the rewritten method is called. If it is not rewritten, the method called is still the parent class's

Same example as above









Guess you like

Origin blog.csdn.net/dream_18/article/details/51532355