Java 07 package visibility modifier accessor method

Keywords: Java Back-end

1 Pack

1.1 declaration of package

Declaration of first level formal package:

package pkg;

For example, create a package named myPackage

package myPackage;

Rule: the directory name must strictly match the package name, as follows:

In fact, the package will include many files, as follows:

Declaration of multi-level package:

package pkg.pkg2.pkg3;

For example:

package java.awt.image;

Note:
package must be the first statement.
Only public classes in other packages can be used.
Classes without defined package names belong to nameless packages and cannot be referenced by named packages.

1.2 introduction of package

The syntax of classes introduced into other packages is as follows:

import Package class.Class name;

Examples are as follows:


Operation results:

analysis:
The package class in the import ch06 package_ In the 1 category, the method of calling Package is called.

Note:
1. Only public classes in other packages can be imported;
2. A class can use multiple import statements to introduce other packages;
3 the import statement is the first non comment statement after the package statement.

1.3 packages commonly used in Java class libraries

There are several packages commonly used in the Java class library. The following categories are highlighted:
java.lang: the basic class library of Java programming language;
java.awt: provides classes for creating user interfaces and drawing and managing graphics and images;
javax.swing: it provides a series of lightweight user interface components and is a commonly used package for Java user interface at present; java.awt.event: graphical user interface event processing package;
java.sql: provides classes for accessing and processing data from Java standard data sources (usually a relational database);
java.io: provides system input and output through data flow, object sequence and file system;
java.util: including collection classes, time processing modes, date and time tools and other common toolkits;
java.net: provides all classes used to implement network communication applications.

2 modifier

Modifier for class 2.1

public: accessible both inside and outside the package
Default: can only be accessed within the package

2.2 modifier for members of class

public: in package + out of package accessible
protected: in package + out of package subclasses are accessible
Default: accessible within the package
private: accessible within the class

Modifiers for class 2.3 methods

public: in package + out of package accessible
protected: in package + out of package subclasses are accessible
Default: accessible within the package
private: accessible within the class
Summary

3 accessor method

Methods and construction methods are generally public
The attribute is generally private
Objects cannot directly access private domains, but users often need to retrieve and modify data domains. In order to access the data field, you can add read methods (getter s) and set methods (setter s) for the private data field. These methods are called accessor methods.

3.1 read - getter method

Setting method
void set attribute name (parameter of attribute type)
For example, for the attribute radius
public void setRadius(double radius)

3.2 setting - setter method

Acquisition method
Property type get property name ()
For example: public double getRadius()

example

Write a Java program. There is a class Telephone in the program. The Telephone class contains Telephone brand, Telephone number, call time, rate, balance attributes, as well as the methods of calculating Telephone charge and displaying information.
When considering recharge, you need to determine how many yuan, so the recharge method needs formal parameters. The balance method after recharging only modifies the attribute value and does not need to return a value.

public class Test {

	public static void main(String[] args) {
		//Instantiate the object and pass in the corresponding value according to the constructor parameters
		Telephone t1;
		t1=new Telephone("Samsung","123456789",100);
		//Set the value of rate
		t1.setRate(0.2);
		//Set talk time
		t1.setDialledTime(10);
		//Object call method
		t1.display();
		t1.callCost();
		System.out.println("Balance before recharge:"+t1.getBanlance());
		t1.recharge(50);
		System.out.println("Balance after recharge:"+t1.getBanlance());
	}

}
//Defines a mobile phone class with 5 attributes
class Telephone {
	private String brand;
	private String number;
	private double dialledTime;//Talk time properties
	private double rate;//Rate, that is, how much is it per minute
	private double banlance;//balance
	//Construction method, 3 parameters
	public Telephone(String brand, String number, double banlance) {
		this.brand = brand;
		this.number = number;
		this.banlance = banlance;
	}
	//get,set method
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public double getDialledTime() {
		return dialledTime;
	}
	public void setDialledTime(double dialledTime) {
		this.dialledTime = dialledTime;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
	public double getBanlance() {
		return banlance;
	}
	public void setBanlance(double banlance) {
		this.banlance = banlance;
	}
	
	//1. How much is the recharge? 2. How much is the balance after the recharge
	public void recharge(double cost) {
		banlance=banlance+cost;
		System.out.println("Balance after recharge"+banlance);
	}
	//Calculate how much it cost and how much money is left
	public void callCost() {
		double callcost=dialledTime*rate;
		banlance=banlance-callcost;
		System.out.println("Telephone charges:"+callcost);
	}
	
	//Display basic information
	public void display() {
		System.out.println("Mobile phone brand:"+brand+"Phone number:"+number);
		System.out.println("Talk time:"+dialledTime+"rate "+rate);
	}
}

Summary

A package is a container for classes.
2 if you write a package statement, it must be the first non comment statement in the file.
3 if you write an import statement, it must be the first non annotated statement after the package statement.
4 visibility modifiers specify the access rights of classes, methods, and properties.
There are two kinds of visibility modifiers for class 5. public is visible inside and outside the package. By default, it can only be visible inside the package.
There are four kinds of member visibility modifiers for class 6, of which public is accessible inside and outside the package; protected is accessible to subclasses inside and outside the package; The default is accessible in the package; private is accessible within a class.
7. The visibility modifiers of class methods are consistent with those of class members.

Posted by justintoo1 on Wed, 27 Oct 2021 04:44:34 -0700