Getting to give up the second week (object-oriented) ...... day.7. . . . . Package, modifiers, keywords;

 

1 package

 

  1. Organization code, avoid naming conflicts
  2. Anti written using the domain name are named package, the package level consistent with the file system

 

 

JDK inside the package:
the java.io: IO processing operation
the java.nio: New IO
the java.lang: Foundation Classes
java.math: arithmetic
java.net: Network Programming
java.sql: relational database operations
java.time: Date Time operation
java.util: a frame set, tools

 

Using classes in the package, the interface ....
Import Package *;.
Import package.class;

 

 

When using different classes in the same package name may be fully qualified class name using

Special attention: the java.lang class, without explicitly import

Static import: import static package.class.staticFiled / staticMethod

 

 

Eclipse; Ctrl + Shift + O optimization introducing
introduced does not recognize the class
is removed but the imported class
import statements according to a certain rule ordering



2, access control modifiers


private: it can modify the properties (members), methods, classes (inner classes), meaning that the modified property or method can only be accessed in this class to achieve the purpose of the package
default: no keyword, you can modify the properties, methods, class, means a modified attribute, method or class, in the present packet only access to
protected, (mostly within the defined call Sons)

public: it can modify the properties (members), methods, classes, meaning modified properties, methods or classes can be accessed any code

 

Design principles: from the most stringent access control (private) begin, then use access control more keywords
actual development: most of the class is public, most of the methods in the class are public

currently recommended: The property must use private, using setter and getter public to get the value.

 

 

On naming attributes: 1, do not use a single letter (eg: n-); 2, not only the use of a lower case initial letter attribute (uName)
recommends: noun 1, 2, beginning at least two letters in lower case 3, a plurality. word, the second word capitalized start

 

1, a user declaration (the User) class, attributes name (name), gender (Gender), date of birth (BIRTHDATE: 1990-01-02)
2, declare a user management (the UserManager) class, which states that a size array 10 users (users) as its attribute
definition method add (user) user sequentially added to users, when the array is full, to produce a new array of size 1.5 times the original array (expansion), the original user and add new users to save in, in fact, gave rise to a dynamic increase in the capacity of an array (ArrayList)

3, write test classes, the main method in Test

public class User {
    
	private String name;
	private String gender;
	private String birthdate;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		
		this.gender = gender;
	}
	public String getBirthdate() {
		return birthdate;
	}
	public void setBirthdate(String birthdate) {
		this.birthdate = birthdate;
	}		
}

  

public class UserManager {
	private User[] users = new User[2];
	private int num = 0;
	public void add(User u) {
		if (num == users.length - 1) {
			User[] x = new User[users.length * 3 / 2];
			for (int i = 0; i < users.length; i++) {
				x[i] = users[i];
			}
			users = x;
		}
		users[num] = u;
		num++;
	}
	public void show() {
		for (int i = 0; i < num; i++) {
			System.out.print(users[i].getName() + " ");
			System.out.print(users[i].getBirthdate() + " ");
			System.out.print(users[i].getGender() + " ");
		}
	}
}

  

 

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserManager userManager=new UserManager();
		User user1=new User();
		User user2=new User();
		user1.setBirthdate("19980101");
		user1.setGender("男");
		user1.setName("suxiao");				
		userManager.add(user1);
		userManager.show();
		System.out.print("\n");
		
		user2.setBirthdate("19980101");
		user2.setGender("男");
		user2.setName("suxiao");
		userManager.add(user2);
		userManager.show();
		System.out.print("\n");
		
		
		User user3=new User();
		user3.setBirthdate("19980101");
		user3.setGender("男");
		user3.setName("suxiao");
		userManager.add(user3);
		userManager.show();
	}

}

  

 

3, static keyword

 


scenes to be used:

1 modified attribute: all instances share the property, no need to instantiate when called directly using the class name property for preservation system global variables.
2 modification method: no need to instantiate when called directly using the class name method, applied to achieve the method of separate tools
3 static import:
4 static code block
5 static inner classes

 

 

4, constructor


Syntax: class name and method name is located exactly the same, there is no return type, and other ordinary methods consistent
effect: initialize the instance: attribute assigned an initial value, reading default parameters ....

Features: If no explicit configuration method, the compiler creates a default constructor with no arguments a, which is consistent with the class modifiers access control
if an explicit constructor, the default constructor with no arguments does not exist

Usage: Use new calls, or use super to call

 

TM:
Features:
1: Register
2, user login
3, check balances
4, deposit
5, withdrawals
6, transfer
7, exit
Class Designer:
User
User Action class
business operations class
global data to save the class
test class

         

public class User {
private String name;
private String password;
private double balance;
private boolean islogin;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public double getBalance() {
	return balance;
}
public void setBalance(double balance) {
	this.balance = balance;
}
public boolean isIslogin() {
	return islogin;
}
public void setIslogin(boolean islogin) {
	this.islogin = islogin;
}

}

  

public class Userpro {	
	public void yanzheng(User u) {
		
		for (int i = 0; i < database.num; i++) {
			if (database.users[i].getName().equals(u.getName())) {
				System.out.println("用户已存在");
				return;
			} 
		}
		database.users[database.num] = u;		
		database.users[database.num].setBalance(0);
		database.num++;
		System.out.print("用户注册成功");		
	}	
	public void login(String name,String password){
		int x=0;
		for (int i = 0; i < database.users.length; i++) {
			if(database.users[i].getName().equals(name)){
				if(database.users[i].getPassword().equals(password)){
					database.users[i].setIslogin(true);
					System.out.print("登陆成功");
					x=1;
					break;
				}					
			}		
	}
		if(x==0){System.out.print("cowu");}
	}		
}

  

 

public class Yewuopr {
	
	public void in(String name, double money) {
		for (int i = 0; i < database.num; i++) {
			if (database.users[i].getName().equals(name)) {
				database.users[i].setBalance(money+database.users[i].getBalance());
				System.out.print("存钱成功");
				break;
			}
		}
	}	
	public void out(String name, double money) {
		for (int i = 0; i < database.num; i++) {
			if (database.users[i].getName().equals(name)) {
				database.users[i].setBalance(database.users[i].getBalance()-money);
				System.out.print("取钱成功");
				break;
			}
		}
	}	
	public void show(){
		for (int i = 0; i < database.num; i++){
			System.out.print(database.users[i].getBalance());		
		}		
	}	
	public void zz(String name, String name2,double money) {
		int p=0;
		int q=0;
		for (int i = 0; i < database.num; i++) {
			if (database.users[i].getName().equals(name)) {
				p=1;q=i;
			}
		}
		if(p==1){for (int i = 0; i < database.num; i++) {
			if (database.users[i].getName().equals(name2)) {
				
				database.users[q].setBalance(database.users[q].getBalance()-money);
				database.users[i].setBalance(database.users[i].getBalance()+money);
					System.out.print("zhuan钱成功");
					break;
			}
		}			
	     }		
	}		
}

  

public class database {
  static  User[] users=new User[10];
  static int num=0;
  
}

  

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
		Userpro userpro=new  Userpro();
		Yewuopr yewuopr=new Yewuopr();
           while(true){
        	   System.out.print("请选择操作   1,注册,2,登录");
        	   int  x=scanner.nextInt();
        	   
        	   if(x==1){
        		   User user1=new User();       		 
        		   System.out.print("用户名");
        		   user1.setName(scanner.next());
        		   System.out.print("密码");
        		   user1.setPassword(scanner.next());        		   
        		   userpro.yanzheng(user1);    
        		   Of System.out.print ( "\ n-"); 
        	   } 
        	   the else { 
        		   the User new new user1 = the User (); 
        		   of System.out.print ( "User Name");       		    
        		   String name = Scanner.next (); 
        		   
        		   of System.out.print ( "password");        		    
        		   String password = Scanner.next (); 
        		   
        		   userpro.login (name, password);       	    
        		   of System.out.print ( "\ n-"); 
        		   the while (to true) { 
        		   of System.out.print ( "select service 1 , deposits, 2, withdrawals, 3, 4 show transfer "); 
        		   int scanner.nextInt X1 = (); 
        		   IF (X1 ==. 1) { 
        			   of System.out.print (" user name amount "); 
        			   String NAME1 = Scanner. next ();
        			   double money=scanner.nextDouble();
        			   yewuopr.in(name1, money);
        			   System.out.print("\n");
        		   }else if(x1==2){
        			   System.out.print("用户名 金额");
        			   String name1=scanner.next();
        			   double money=scanner.nextDouble();
        			   yewuopr.out(name1, money);
        			   System.out.print("\n");
        		   }else if(x1==4){
        			   yewuopr.show();
        			   System.out.print("\n");
    		       }else if(x1==3){
    		    	   System.out.print("用户名 用户名 金额");
        			   String name1=scanner.next();
        			   String name2=scanner.next();
        			   double money=scanner.nextDouble();
        			   yewuopr.zz(name1,name2, money);
        			   System.out.print("\n");    		    	   
    		       }
        		        		           		   				}      		   
        		   }       		         	   
        	   }       	   
           }
	}

  

 

 

 

 

 

 

 

 

 

                                               Now a job to write long. . . . . . . . . . . . He looked tired

                                       

 

 

       

 

 

 

 

 

 

 

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/suxiao666/p/11334486.html