JAVA basics | understanding of construction methods

Definition of construction method

The difference between the construction method and the ordinary method is that it is generated in the class, so it is used to initialize the object members when constructing the object, and its name is the same as the name of the class to which it belongs

Implementation of the construction method

The construction method is implemented in the class, so there is a class
such as

class 类名
	{
    
    
	访问权限 类名(参数1,参数2...参数n)
	{
    
    
		构造方法体
	}
}

Features: The access rights are divided into three types: public private protect. The
body name and class name of the construction method are the same.
There is no return type. This is the biggest difference from ordinary methods.因为,构造函数用来初始化对象成员,而对象成员类型可以有很多,所以构造函数无需返回类型

The following features have not been encountered, and then strengthen the
construction method. It can be overloaded. The
construction method cannot be modified by static and final. The
construction method cannot be inherited. Subclasses need to use the keyword super to use the construction method of the parent class.

A major feature of the
construction method is that it is automatically executed "implicitly" when creating an object, so the construction method does not need to be called directly in the program, but is automatically executed once when the object is generated.

I brought it when I just started writing ; remember that it doesn’t need to be brought here like the function; this is the result of the construction method at the end
Insert picture description here

public class ConstructTest01
{
    
    
	public static void main(String[] args)
	{
    
    
		Person P=new Person(12);
		P.show("被调用!");
	}
}
//在此类中构造一个方法:
class Person
{
    
    
		
	public Person(int x)//
	{
    
    
		a=x;
		System.out.println("构造方法被调用");
		System.out.println("a="+a);
	}

	public void show(String str)
	{
    
    
		System.out.println("str="+str);
	}
	private int a;
}

One more look


class Book1
{
    
    
	String title;
	double price;
	static String pub="出版社";
	void print()
	{
    
    
		System.out.println("书名:"+title);
		System.out.println("价格:"+price);
		System.out.println("出版社:"+pub);
	}
	public Book1(String t,double pr,String pu)
	{
    
    
		title=t;
		price=pr;
		pub=pu;
		System.out.println("被调用");
	}
}

public class Book
{
    
    
	public static void main(String[] args)
	{
    
    
		
		Book1 B=new Book1("图书馆",87,"酒楼");
		System.out.println("fenge");
		B.print();
	}
}

Constructor privatization

Example
A private construction method is defined in the PrivateDemo class

public class PrivateDemo
{
    
    
	private PrivateDemo()
	{
    
    
	}
}

If you want to instantiate the constructor of the above class in another class, an execution error
will occur, because the privatized constructor cannot be visible in other classes

public class PrivateCallDemo
{
    
    
	public static void main(String[] args)
		{
    
    
			PrivateDemo Demo=new PrivateDemo();//私有化的构造方法不能在一个新类中调用。
		}
}

The privatized construction method can only be called in this class

Example 2

public class PrivateDemo
{
    
    
	String name;
	private PrivateDemo()//私有化的构造方法
	{
    
    
		name="GGGG";
		System.out.println("name: "+name);
	}

	//在同一个类中,调用上面这个私有化的构造方法
	public void main(String[] args)
	{
    
    
		new PrivateDemo();//在本类中可以调用
	}
}

Example 3 how other classes indirectly call the privatized construction method

It is also called through the public method of the same class as privatization

public class PrivateDemo
{
    
    
	String name;
	private PrivateDemo()//私有化的构造方法
	{
    
    
		name="GGGG";
		System.out.println("name: "+name);
	}

	//同类中调用私有化的构造方法
	public static final PrivateDemo PRI=new PrivateDemo();

	//上面在同类中调用了私有化的构造方法,
	//下一步再创建一个普通方法来调用该对象PRI即可
	public static PrivateDemo GetPri()
	{
    
    
		return PRI;
	}
}

Call the ordinary method of the above class in other classes, and indirectly realize the call of the privatized construction method

public class TestPrivate
{
    
    
	public static void main(String[] args)
	{
    
    
		PrivateDemo P=PrivateDemo.GetPri();
	}
}

Insert picture description here
It realizes that Class B calls the privatization construction method 1 through the ordinary method 2 in Class A, so that class B indirectly calls the privatization construction method in Class A.

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/114446931