九、关键字:this的使用
package com.atguigu.java2;
/*
* this关键字的使用
* 1. this可以用来修饰、调用:属性、方法、构造器
*
* 2. this修饰属性和方法:
* this理解为:当前对象 或 当前正在创建的对象
*
* 2.1 在类的方法当中,我们可以使用"this.属性"或"this.方法"的方式,调用当前对象的属性或方法
* 但是,通常情况下,选择省略"this."。特殊情况下,如果方法的形参和属性同名,我们必须显示使用
* "this.变量"的方式,表明此变量是属性,而非形参
*
* 2.2 在类的构造器当中,我们可以使用"this.属性"或"this.方法"的方式,调用当前对象的属性或方法
* 但是,通常情况下,选择省略"this."。特殊情况下,如果构造器的形参和属性同名,我们必须显示使用
* "this.变量"的方式,表明此变量是属性,而非形参
*
* 3. this调用构造器
* 3.1 我们在类的构造器中,可以显示的使用"this(形参列表)"方式,调用本类中指定的其他构造器
* 3.2 构造器中不能通过"this(形参列表)"方式,调用自己
* 3.3 如果一个类中有n个构造器,则最多有n-1构造器中使用"this(形参列表)"
* 3.4 规定:"this(形参列表)"必须在当前构造器首行
* 3.5 构造器内部,最多只能声明一个"this(形参列表)",用来调用其他的构造器
*/
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setAge(1);
System.out.println(p1.getAge());
p1.eat();
System.out.println();
Person p2 = new Person("Jerrmy",20);
}
}
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;
//Person初始化时,需要考虑如下1,2,3,4(共40行代码)
}
public Person(int age){
this();
this.age = age;
//Person初始化时,需要考虑如下1,2,3,4(共40行代码)
}
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 name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void eat(){
System.out.println("人吃饭");
this.study();
}
public void study(){
System.out.println("人学习");
}
}
9.1 练习11
package com.atguigu.exer2;
public class Boy {
private String name;
private int age;
public Boy() {
}
public Boy(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void marry(Girl girl){
System.out.println("我想娶" + girl.getName());
}
public void shout(){
if(this.age >= 22){
System.out.println("你可以登记结婚了");
}else{
System.out.println("多谈谈恋爱");
}
}
}
package com.atguigu.exer2;
public class Girl {
private String name;
private int age;
public Girl(){
}
public Girl(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void marry(Boy boy){
System.out.println("我想嫁给" + boy.getName());
boy.marry(this);//传递的是Girl类型,this表示当前对象
}
public int compare(Girl girl){
/*if(this.age > girl.age){
return 1;
}else if(this.age < girl.age){
return -1;
}else {
return 0;
}*/
return this.age - girl.age;
}
}
package com.atguigu.exer2;
public class BoyGirlTest {
public static void main(String[] args) {
Boy boy = new Boy("罗密欧", 21);
boy.shout();
Girl girl = new Girl("朱丽叶", 18);
girl.marry(boy);
Girl girl1 = new Girl("祝英台", 18);
int compare = girl.compare(girl1);
if(compare > 0){
System.out.println(girl.getName() + "大");
}else if(compare > 0){
System.out.println(girl1.getName() + "大");
}else{
System.out.println("一样大");
}
}
}
9.2 实验1
9.3 实验2(难、收获大)
1.按照如下的 UML 类图,创建相应的类,提供必要的结构
在提款方法 withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,
应给出提示。deposit()方法表示存款。
2.按照如下的 UML 类图,创建相应的类,提供必要的结构
按照如下的 UML 类图,创建相应的类,提供必要的结构
addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer 对象,然后把
它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。getNumOfCustomers 方法返回 numberofCustomers 属性值。
getCustomer 方法返回与给出的 index 参数相关的客户。
创建 BankTest 类,进行测试。
Account
package com.atguigu.exer4;
public class Account {
private double balance;
public Account(double init_balance){
this.balance = init_balance;
}
public double getBalance(){
return balance;
}
//存钱操作
public void deposit(double amt){
if(amt > 0){
balance += amt;
}
}
//取钱操作
public void withdraw(double amt){
if(balance >= amt){
balance -= amt;
System.out.println("取钱成功");
}else{
System.out.println("余额不足");
}
}
}
Bank类
package com.atguigu.exer4;
public class Bank {
private Customer[] customers;//存放多个数组
private int numerOfCustomer;//记录客户个数
public Bank(){
customers = new Customer[10];
}
//添加客户
public void addCustomer(String f,String l){
Customer cust = new Customer(f,l);//new一个,创建对象
//报空指针异常,数组没有初始化
customers[numerOfCustomer++] = cust;//存放地址
}
//获取指定位置上客户
public Customer getCustomer(int index) {
if(index >= 0 && index <= numerOfCustomer){
return customers[index];
}
return null;
}
public int getNumerOfCustomer() {
return numerOfCustomer;
}
}
Customer类
package com.atguigu.exer4;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l){
this.firstName = f;
this.lastName = l;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
BankTest类
package com.atguigu.exer4;
public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("Jane","Smith");
//匿名对象
bank.getCustomer(0).setAccount(new Account(2000));
bank.getCustomer(0).getAccount().withdraw(500);
double balance = bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户 :" + bank.getCustomer(0).getFirstName() +
"账户的余额为:" + balance);
System.out.println("***************************");
bank.addCustomer("万里", "鹏程");
System.out.println("银行客户个数为 " + bank.getNumerOfCustomer());
}
}
十、关键字:package、 import的使用
package com.atguigu.java2;
//import的落脚点是类或接口
import java.lang.reflect.Field;
import java.util.*;
import com.atguigu.exer4.Account;
import com.atguigu.java2.java3.Dog;
//import static的落脚点是类中的结构
import static java.lang.System.*;
import static java.lang.Math.*;
/*import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;*/
/*
* 一、package关键字的使用
* 1.为了更好的实现项目中类的管理,提供包的概念
* 2.使用package声明类或接口所属的包,声明在源文件的首行
* 3.包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、“见名知意”
* 4.每“.”一次,就代表一层文件目录。
*
* 补充:同一个包下,不能命名同名的接口、类
* 不同包下,可以命名同名的接口、类
*
* 二、import关键字的使用
* import:导入
* 1.在源文件中显示的使用import结构导入指定包下的类、接口
* 2.声明在包的声明和类的声明之间
* 3.如果需要导入多个结构,则并列写出即可
* 4.可以使用"xxx.*"的方式,表示可以导入xxx包下的所有结构
* 5.如果使用的是类或接口是java.lang包下定义的,则可以省略import结构
* 6.如果使用的类或接口是本包下定义的,则可以省略import结构
* 7.如果在源文件中,使用不同包下的同名的类则必须至少有一个类需要以全类名的方式显示
* 8.使用"xxx.*"方式表明可以调用xxx包下的所有结构。但如果使用的是xxx子包下的结构,则
* 仍需要显示调用,"xxx.*"指表示xxx包下的类或接口,不表示子包
*
* 9.import static:导入指定类或接口中的静态结构:属性或方法
*/
public class PackageImportTest {
public static void main(String[] args) {
String info = Arrays.toString(new int[]{1,2,3});
ArrayList list = new ArrayList();
HashMap map = new HashMap();
Scanner s = null;
System.out.println("hello");
Account acct = new Account(1000);
//全类名的方式显示
//com.atguigu.exer3.Account acct1 = new com.atguigu.exer3.Account(1000,2000,0.0123);
//要导入,应为是com.atguigu.java2的子包下的结构
Dog dog = new Dog();
//lang包不用导,但是lang包下的子包仍然需要导包,和上一行一样
Field field = null;
//导入import static java.lang.System.*;
out.println();
//import static java.lang.Math.*;
//long num = Math.round(123.45);
long num = round(123.45);
}
}