[hard study] Java notes 0.7

Keywords: Java Database JSP

Java notes

One of the object-oriented features: encapsulation and hiding

The user's direct operation on the properties (member variables of the object) defined inside the class will lead to data errors, confusion and security problems.
In Java, you can declare the data as private, and then provide public methods: getXxx() and setXxx() to implement the operation of this property, so as to achieve the following purposes:
Hide the implementation details in a class that do not need to be provided externally;
Users can only access data through pre customized methods, and can easily add control logic to limit the unreasonable operation of attributes;
Easy to modify and enhance code maintainability;

public class Person {
//	public int age; / / in this case, it is necessary to develop the properties of the class so that callers can use them freely. This may cause problems
	
	//We need to encapsulate and hide attributes that cannot be used by callers at will
	/**
	 * First, declare the property settings as private, and use the private keyword
	 * Set and get properties by writing setXxx () and getXxx () methods of public type
	 */
	private int age;
	
	public void printAge() {
		System.out.println("Age:" + age);
	}
	
	
	public int getAge() {
		return age;
	}
	
	
	public void setAge(int a) {
		if(a <= 150 && a >=0) {
			age = a;
		}else {
			System.out.println("Age entered:" + a + "Not between 0 and 150");
		}
	}
}

Four access modifiers

The Java permission modifiers public, protected, and private are placed before the member definition of the class to restrict the access of the object to the members of the class.

Modifier Class internal Same package Subclass Anywhere except as before
private Yes
Default (default) Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes
For class permission decoration, only public and default (default) can be used
public classes can be accessed anywhere
The default class can only be accessed by classes within the same package
If the subclass and the parent are in the same package, then only the member modifier of the parent is not private, then the subclass can use
If the subclass and the parent are in different packages, the subclass can only use the protected and public modified members of the parent

Member 3 of class: constructor (constructor)

Characteristics of the constructor:
It has the same name as the class.
It does not declare a return value type. (different from declared void)
Cannot be modified by static, final, synchronized, abstract, native, or return statement

The role of the constructor: creating objects; initializing objects
For example: Order o = new Oreder(); Person p = new Person (Peter, 15);
Just as we stipulate that every "person" must take a bath at birth, we can add the program code to complete the "bath" in the construction method of "person", so that every "person" will automatically complete the "bath" at birth, and the program does not have to tell each person to "take a bath" one by one at birth.

	class T0{
	//Default construction method
	T0(){}
}

public class Person {
	//Whether there is a modifier accessed in front of the default construction method is related to the defined class. The class is public. The default construction method is public. The default class is default. Its construction method is default
	public Person() {}

Syntax format:
Modifier class name (parameter list)
Initialization statement;

According to different parameters, constructors can be divided into the following two categories:
Implicit parameterless constructor (provided by default)
Explicitly define one or more constructors (no arguments, with arguments)
The new object is actually the constructor of the calling class (default parameterless constructor)

be careful:
In the Java language, each class has at least one constructor
The modifier for the default constructor is the same as the modifier for the class to which it belongs
Once the constructor is explicitly defined, the system no longer provides a default constructor
A class can create multiple overloaded constructors
The constructor of the parent class cannot be inherited by the child class

package day06;

public class Person5 {
	
	public int age;
	public String name;
	public void showInfo() {
		System.out.println(age);
		System.out.println(name);
	}
	/**
	 * If the constructor is explicitly defined, then the explicit constructor is used
	 */
//	public Person5() {
//		age = 11;
//		
	/**
	 * The construction method of writable band parameters
	 * @param a
	 * @param n
	 */
	public Person5(int a, String n) {
		age = a;
		name = n;
	}
		
}

Constructor Overload

Constructors are also called construction methods
Constructors are generally used to create objects while initializing them.

Constructor overloading makes the creation of objects more flexible and convenient to create various objects.

package day06;

public class Person6 {
	/**
	 * The overload of constructors is to make it easy to call methods and create objects with different needs flexibly
	 * To overload multiple construction methods is to provide multiple templates for initializing new objects
	 */
	public Person6(){
		
	}
	
	public Person6(int a){
			
	}
	
	
	public Person6(String n){
		
	}
	
public Person6(int a,String n){
		age = a;
		name = n;
	}
	
	public int age;
	public String name;
}

Constructor overload, parameter list must be different.

Keywords - this

What is this?
In Java, this keyword is difficult to understand, and its function is very close to its meaning.
It is used inside a method, that is, a reference to the object to which the method belongs;
It is used inside the constructor to represent the object that the constructor is initializing.

this represents the current object and can call the properties, methods and constructors of the class

When to use this keyword?
Use this when the object that calls the method needs to be used within the method

package day06;

public class Person7 {
	public Person7() {
		
	}
	
	public Person7(int age) {
		this.age = age;	
	}
	
	public Person7(String name) {
		this();//Equivalent to calling public Person7()
		this.name = name;
	}
		
	public Person7(int age,String name) {
		this(1);//Equivalent to calling public person 7 (int age)
		this.age = age;
		this.name = name;
	}
	int age;
	String name;
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setName1(String name) {
		this.setName(name);
	}
	
	public void showInfo() {
		System.out.println("full name:" + this.name);
		System.out.println("Age:" + this.age);
	}
}

be careful:
1. Use this () must be on the first line of the constructor!
2. Use this to call other constructors in this class to ensure that at least one constructor does not use this. (in fact, the constructor cannot call itself)

JavaBean

JavaBean is a reusable component written in Java language

JavaBeans refer to Java classes that meet the following standards:
1. Class is public
2. There is a common constructor without parameters
3. There are attributes, which are generally private, and there are corresponding get and set methods.

Users can use JavaBeans to package functions, processes, values, database access and any other objects that can be created by java code, and other developers can use these objects through internal JSP pages, servlets, other JavaBeans, applet programs or applications. Users can think of JavaBeans as providing a copy and paste capability anytime, anywhere, without caring about any changes.

package day06;
/**
 * A javabean
 * Private properties
 * get and set methods corresponding to properties
 * @author JJ
 *
 */

public class Person8 {
	private String name;//full name
	private int sex;//Gender, 0 male and 1 female
	private int age;//Age
	
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int getSex() {
		return this.sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	
	public int getAge() {
		return this.age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
	
}

Posted by terje-s on Tue, 09 Jun 2020 21:11:26 -0700