Java study notes five (use the package and a few keywords)

An object-oriented features: encapsulation

The introduction of the problem: When we create a class, we can instantiate the class attributes of the class assignment. In this case, assignment is limited only by the scope of data storage type and properties, there are no other restrictions. But in practical problems, we may also have some other restrictions, but these limits are not reflected in the property declaration, we can only add restrictions by the process, and then to prevent the user through the object . The method properties in the assignment of property, you need to take property declared private (private). At this time, the attribute of the reflected encapsulation.

Reflect the encapsulation: property privatization (Private) class we will then provide a common (public) method sets (set) and acquire values (GET) properties.
Note: In addition to the above-mentioned properties privatization, encapsulation method and also in private Singleton not exposed outside.

Permissions modifier : four permission modifier Java prescribed in ascending order in turn is private, default, protected, public.
These four rights modifier can modify the internal members of the class and class; specifically, the class can be modified: properties, methods, constructors, inner classes; public and can only be used by default both modifiers when modified class.
Courseware four permission range modifiers : private visible, the default can be seen, protected can be seen, only visible in the public class between a package in the same class in different subclasses packet between the different packets;

Summary encapsulation : Java provides four modifiers permission to modify the internal members of the class and category, reflecting the size of the internal members of the classes and visibility of the external when called.
Examples are as follows:

public class MyAnimalTest{
	public static void main(String[] args) {
		Animal1 ani = new Animal1();
		System.out.println(ani.getLeg());
		ani.setLeg(4);
		System.out.println(ani.getLeg());
		ani.setLeg(5);
		System.out.println(ani.getLeg());
	}
}
class Animal1{
	String name;
	private int age;
	private int leg;
	
	public void setLeg(int l) {
		if(l > 0 && l%2==0) {
			this.leg = l;
			
		}else {
			System.out.println("Error");
		}
	}
	public int getLeg() {
		return leg;
	}
}

The inner member of the class constructor (constructor)

The role of the constructor :
1. Create an object
2, object initialization information

Structure constructor: permission modifier class name (parameter list);
few notes:
1, provided is not a class constructor, the system provides a default constructor empty reference;
2, once the class provides the builder system will no longer provide an empty argument constructor;
3, constitutes the same class constructor overloads between the different parameter list;
4, each class will have at least one builder;

this keyword to create an object can be understood or intended to create.

public class MypersonTest {
	public static void main(String[] args) {
		
		Person1 p = new Person1();
		Person1 p1 = new Person1("shenshuo",24);
		System.out.println(p.getName()+","+p1.getName());
		p.setName("花花");
		System.out.println(p.getName()+","+p1.getName());
	}

}
class Person1{
	private String name;
	private int age;
	
	public Person1() {
		
	}
	
	public Person1(String name,int age) {
		this(); // 调用前面的构造器
		this.name = name;
		this.age = age;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return age;
		
	}
	public void eat() {
		System.out.println("人吃饭");
	}
	public void study() {
		System.out.println("人可以学习");
	}
	
	
}

JavaBean is a Java language class reusable components written in

Called JavaBean Java classes meet the following criteria:
1, a common class (i.e., with a public modified class)
2, a non-parametric public constructor (i.e. public modified without reference constructor)
3, property, and provides attributes get, set method

public class Customer {//公共的类
	
	private int id;
	private String name;//属性
	
	public Customer(){//公共的无参构造器
		
	}
	
	public void setId(int i){
		id = i;
	}
	public int getId(){//对应属性的get、set方法
		return id;
	}
	public void setName(String n){
		name = n;
	}
	public String getName(){
		return name;
	}
	
}

Property assignment of the order:

Followed by:
① default initialization
② explicitly initialized
③ constructor initializes
④ through the object. Property, object. Assignment of the method
①> ②> ③> ④ which is greater than the number in the earlier meaning.

Three keywords this, package and import:

using this keyword :
this keyword can be used to modify: properties, methods, constructors;

this modification to the current understanding of the properties and methods of an object, this modified structure is interpreted as the current object being created.

In the method of class : this can be called by the properties and methods of this property, this embodiment of the method, but generally this is not written is omitted... In special cases, if the attribute name parameter name and class methods are the same, this is required to display a description of this attribute is not a variable parameter.
In the constructor : You can use this property, this ways and means to call the properties and methods of the current object being created, but usually this omission does not write, in exceptional cases, if the parameter name in the constructor and class. attribute names are the same, this is required to display a description of this attribute is not a variable parameter.

this calls the constructor : this class can call other configurations specified by this (parameter list) manner.
Few things to note:
1, the this (parameter list) can be placed only constructor of the first line, but not in this way to call itself.
2, each constructor can only declare a this (parameter list) to call other constructors.
3, if there are n this class constructor, there are only a maximum of n-1 constructor uses this (parameter list) to call other constructors;

class Person{
	
	private String name;
	private int age;
	
	
	public Person(){
		
//		this.eat();
		String info = "Person初始化时,需要考虑如下的1,2,3,4...(共40行代码)";
		System.out.println(info);
	}
	
	public Person(String name){
		this();
		this.name = name;
		
	}
	
	public Person(int age){
		this();
		this.age = age;
		
	}
	
	public Person(String name,int age){
		this(age);
		this.name = name;
		//this.age = age;
		//Person初始化时,需要考虑如下的1,2,3,4...(共40行代码)
	}
	
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return this.name;
	}
	public void setAge(int age){
		this.age = age;
	}
	public int getAge(){
		return this.age;
	}
	
	public void eat(){
		System.out.println("人吃饭");
		this.study();
	}
	public void study(){
		System.out.println("人学习");
	}
	
}

using the package keyword :
In order to better manage the project in class, the concept of the package;
the use of packages, declare the package statement in the class or interface belongs to the first line of the source file;
packet also belong identifier, to be followed logo symbol naming convention, package names are in lowercase letters, but also to see the name do know meaning;
which, no . once, on behalf of a layer of the file directory
Note : the same package can not have a class or interface with the same name, different packages the class or interface can have the same name.

import using keywords :( introducing)
use import structures to take care to import classes and interfaces specified in the source file;
import generic declaration statement between the package and the like;
few notes :
1, if it is present in the package or java.lang classes and interfaces in the package, there is no need to import using the import;
2, if there are a plurality of package structures need to import, can be written in parallel;
3, if there are two classes of the same name in different packages , you need to have at least one class in the use of the display by way of the full class name.
4, may be used "xxx. *" In the structure in a manner xxx import all packets. However, if the lower sub-packet structure when the package is to be displayed is also introduced into the sub-packets.
import static class or interface for introducing static structure (attributes and methods);
for example:

package com.atguigu.java2;

import java.lang.reflect.Field;
Published 11 original articles · won praise 14 · views 5455

Guess you like

Origin blog.csdn.net/ssnszds/article/details/104475219