Java learning 5 - Book self-study

Keywords: Java

Chapter VII         Features of Java language classes

7.1 private and public members of class

7.1.1 private members

If there is no mechanism to restrict access to members in a class, it is likely to cause incorrect input. To prevent this from happening, the Java language provides the access control character private.

After adding the private access control character, the internal members of the class cannot be accessed from outside the class. Achieve the highest level of protection for member data.

7.1.2 public members

Java provides the public access control character public. If you prefix the member declaration of a class with the modifier public, it means that the member can be accessed by all other classes. Because the public modifier will reduce the security and data encapsulation, the use of public members should be reduced.

7.1.3 default access control character

If no access control character is added to the front of a class member, the member has the default access control character feature. This default access control right means that this member can only be accessed and called by classes in the same package (Class Library). If a subclass is in a different package from its parent class, the subclass cannot access the default access control member in the parent class, that is, no class in any other package can access the default access control member.

Similarly, for a class, if a class does not have an access control character, it indicates that it has the characteristics of default access control. This default access control right stipulates that it can only be accessed and referenced by classes in the same package, but not used by classes in other packages.

7.2 overloading of methods

Method overloading is a method to realize "polymorphism". In object-oriented programming language, some methods have the same meaning but with different parameters. These methods use the same name, which is called method overloading.

In other words, overloading refers to multiple methods with the same name in the same class. If the number of parameters of these methods with the same name is different, or the number of parameters is the same, but the types are different, these methods with the same name have different functions.

//filename:App7_3.java

class Cylinder
{
	private double radius;
	private int height;
	private double pi=3.14;
	private String color;
	public double setCylinder(double r, int h)
	{
		radius=r;
		height=h;
		return r+h;
	}
	public void setCylinder(String str)
	{
		color=str;
	}
	public void show()
	{
		System.out.println("the color of cylinder is :"+color);
	}
	double area()
	{
		return pi*radius*radius;
	}
	double volume()
	{
		return area()*height;
	}
}
public class App7_3
{
public static void main(String[] args)
{
	double r_h;
	
	Cylinder volu=new Cylinder();
	r_h=volu.setCylinder(2.5, 5);
	volu.setCylinder("red");
	System.out.println("the sum of cylinder's radius and height is:"+r_h);
	System.out.println("the volume of cylinder is:"+volu.volume());
	volu.show();
}
}

Note: the java language does not allow overloads with the same number of parameters or parameter types, but only different return value types.

7.3 construction method

The Java language provides a special member method in a class -- a constructor.

7.3.1 function and definition of construction method

constructor is a special method that initializes the members of an object when it is created.

The name of a constructor must be exactly the same as the name of its class. A constructor has no return value, but when defining a constructor, the constructor name cannot be decorated with the modifier void because the return value type of the constructor of a class is the class itself. After the constructor is defined, it will be called automatically when creating an object, so the constructor does not need to be modified It should be called directly in the program, but automatically executed when the object is generated.

This is different from general methods, which are called when needed.

//filename App7_4.java
class Cylinder
{
	private double radius;
	private int height;
	private double pi=3.14;
	public Cylinder(double r, int h)
	{
		radius=r;
		height=h;
	}
	double area()
	{
		return pi*radius*radius;
	}
	double volume()
	{
		return area()*height;
	}
	
}
public class App7_4
{
	public static void main(String[] args)
	{
		Cylinder volu=new Cylinder(3.5,8);
		System.out.println("the area of cylinder is:"+volu.area());
		System.out.println("the volume of cylinder is:"+volu.volume());
	}
}

Note: if you add void when constructing a method, it will become a normal method.

7.3.2 default construction method

There is no constructor defined in the code, but you can still create new objects and execute the program correctly. This is because if you omit the constructor, the Java compiler will automatically generate a default constructor for this class. There is no code in this method body.

If the class is preceded by the public modifier, the default constructor is also public.

Once the user defines the constructor for this class, the system will no longer provide the default constructor, which is caused by Java coverage.

7.3.3 overloading of construction method

Generally, a class has one or more construction methods. However, because the construction method has the same name as the class, when a class has multiple construction methods, these construction methods can be overloaded. As long as the number of parameters between methods is different or the type of parameters is different, multiple methods with the same name can be constructed.

7.3.4 define another construction method from one construction method

For some specific operations, the Java language allows calling another constructor from one constructor within a class. Using this method can shorten program code and reduce program development time. Calling another constructor from a dog miscellaneous method is called using the this() statement.

7.3.5 public construction method and private construction method

Constructors are generally public because they are automatically called outside the class when creating objects. If a constructor is declared private, it cannot be called outside the class where the constructor is located, but it can still be called inside the class.

//filename: App7_7.java
class Cylinder
{
	private double radius;
	private int height;
	private double pi=3.14;
	String color;
	private Cylinder()
	{
		System.out.println("THe no-argument constructor is called.");
		
	}
	public Cylinder(double r,int h,String str)
	{
		this();
		radius=r;
		height=h;
		color=str;
	}
	public void show()
	{
		System.out.println("the vase radius of the cylinder is:"+radius);
		System.out.println("the height of the cylinder is:"+height);
		System.out.println("the color of cylinder is :"+color);
		
	}
	double area()
	{
		return pi*radius*radius;
	}
	double volume()
	{
		return area()*height;
	}
	
}
public class App7_7
{
	public static void main(String[] args)
	{
		Cylinder volu=new Cylinder(2.5,5,"blue");
		System.out.println("the area of cylinder is:"+volu.area());
		System.out.println("the volume of cylinder is:"+volu.volume());
		volu.show();
		
	}
}

Note: this () is only involved in the mutual call between construction methods, so it is relatively clear.

7.4 static members

Static is called a static modifier, which can modify members in a class. Members modified by static are called static members, also known as class members. Members not modified by static are called instance members.

7.4.1 instance members

In the class definition, if the member variable or member method is not modified by static, the member is an instance member, which leads to instance variables, instance methods, etc.

7.4.2 static variables

Variables decorated with static are called static variables, and static variables are also called class variables. Static variables are variables belonging to a class, not specific objects belonging to any class. That is, for any specific object of the class, static variables are a public storage unit, not saved in the memory space of an object instance, but in the class In the public storage space of the memory.

Static variables have fixed storage space, which is similar to global variables in other languages to some extent. If they are not private, they can be accessed externally without creating an instance object of the class. They can be referenced only by the class name. In other words, static variables can be referenced without instantiation.

There are two ways to use static variables:

Class name. Static variable name;

Object name. Static variable name;

If there are static variables in the class, the static variables must be independent of the method, just as other high-level languages must be declared outside the function when declaring the global variables.

Note: for the use of static variables, it is recommended to access them in the form of "class name. Static variable name"

7.4.3 static method

The method modified with the static modifier is called the static method of the class, also known as the class method. Static methods are essentially methods belonging to the whole class, rather than static methods. They are methods belonging to a specific object. Declaring an object static has the following meanings:

1) A non static method is a method of an object. When the object is created, the method of the object has its own code segment in memory. The static method belongs to the whole class, and its code segment in memory will be shared by all objects, not dedicated to any object;

2) Because static method belongs to the whole class, it cannot manipulate and process members belonging to an object, but can only process members belonging to the whole class, that is, static method can only access static member variables or call static member methods, or instance variables and instance methods cannot be accessed in static methods.

3) This or suer cannot be used in static methods. Because this represents the object that calls the method, since the "static method" does not need an object to call, this should not exist inside the "i ontai method".

4) When calling static methods, you can call them directly with the class name or with a specific object name. The format is as follows:

Class name. Static method name ();

Object name. Static method name ();

Static methods can be called directly by class name without generating objects.

Note: for the use of static methods, it is recommended to access them in the form of "class name. Static method name ()".

If a class is loaded and run by the Java Virtual Machine Interpreter, since the Java virtual machine runs from main(), this class must have a main() method. Since the Java virtual machine needs to call the main() method outside the class, the access permission of this method must be public. Before starting to execute a program, the Java virtual machine runtime system does not create an instance object of the class of the main() method, so it can only call the main() method as the entry of the program through the class name, that is, the class that calls the main() method, not the object created by the class, so the method must be static.

7.4.4 static initializer

A static initializer is a statement group enclosed by a pair of braces {} modified by the keyword static. Its function is similar to the class construction method. It is used for initialization, but the static initializer is fundamentally different from the construction method.

1) The constructor initializes each class created, while the static initializer initializes the class itself;

2) The construction method is automatically executed by the system when creating a new object with the new operator, while the static initializer can not be called by the program. It is executed by the system call when the class it belongs to is loaded into memory.

3) The constructor is called as many times as many new objects are created with the new operator, but the static initializer is only executed once when loaded into memory, regardless of how many objects are created.

4) Unlike constructors, static initializers are not methods and therefore have no method name, return value, and parameters.

Note: if there are multiple static initializers, it is automatically completed when the system loads into memory.

7.5 application of objects

Variables can be divided into "basic type variables" and "non basic type variables". When declaring basic type variables, the format is: "data type variable name", such as int a, double b, etc. while the format of declaring an object is similar to it, that is, "class name object name", so the object can be called "class type variables" ", which is a variable of non basic type. In fact, an object is a reference variable, and a reference variable actually stores the first address of the object in memory (also known as the handle of the object). Therefore, in terms of the function of the object, the object is an "object pointing to the object", but in terms of its type, it is a "class type variable". Therefore, in some cases, the object can be used as a basic type.

7.5.1   Assignment and comparison of objects

When using an object, you usually create an object with the new operator first, and then operate on it. However, sometimes you can still assign a value to a new object without using the new operator.

7.5.2 reference variables as return values of methods

A reference variable can be passed not only as a parameter, but also as the return value of a method. To return a variable of class type, you only need to add the class to be returned before the method declaration.

7.5.3   Array of class types

Array elements can store various types of data. Of course, arrays can also be used to store objects. Using arrays to store objects generally goes through the following two steps:

1) Declare array variables of type, and allocate memory space to the array with new operator;

2) Create an array type object with new, allocate memory space to it, and let the array elements point to it.

//filename: App7_13.java
class Person
{
	private String name;
	private int age;
	public Person(String name, int age)
	{
		this.name=name;
		this.age=age;
	}
	public void show()
	{
		System.out.println("name:"+name+"    age:"+age);
		
	}
}
public class App7_13
{
public static void main(String[] args)
{
	Person[] per;
	per=new Person[3];
	per[0]=new Person("zhangsan",19);
	per[1]=new Person("lisi",20);
	per[2]=new Person("wangwu",40);
	per[2].show();
	per[0].show();
}

  7.5.4 method call with object array as parameter

Arrays can also be used to hold objects, so you can pass an object array as a parameter to a method.

//filename: App7_14.java
class Person
{
	private int age;
	private String name;
	public Person(String name, int age)
	{
		this.name=name;
		this.age=age;
	}
	public static int minAge(Person[] p)
	{
		int min=Integer.MAX_VALUE;
		for(int i=0;i<p.length;i++)
			if(p[i].age<min)
				min=p[i].age;
		return min;
	}
}
public class App7_14
{
	public static void main(String[] args)
	{
		Person[] per= new Person[3];
		per[0]=new Person("zhagnsan",20);
		per[1]=new Person("lisi",21);
		per[2]=new Person("wqangwu",19);
		System.out.println("the minimum age is: "+Person.minAge(per));
	}
}

Posted by nalleyp23 on Fri, 19 Nov 2021 15:22:02 -0800