this关键字
this修饰属性、方法:
package com.atguigu.java2;
/*
*this关键字的使用:
*1,this可以用来修饰:属性、方法、构造器
*2,this修饰属性和方法:
* this理解为:当前对象(类的方法中)或当前正在创建的对象(构造器中)
* 2.1,在类的方法(构造器)中,我们可以使用“this.属性”或“this.方法”的方式,调用当前对象(当前正在创建)的属性或方法;通常我们会省略this.
* 但特殊情况向下,如果方法(构造器)的形参和类的属性同名时(区分不开),必须显式地使用“this.变量”的方式,表明此变量是属性,而非形参。
*/
public class PersonTest {
public static void main(String[] args) {
Person p1=new Person();
p1.setAge(1);
System.out.println(p1.getAge()); //结果为1
}
}
//构造器
class Person{
private String name;
private int age;
public Person(){
}
public Person(String name){
this.name=name; //前面属性,后面形参
}
public Person(int age){
this.age=age; //前面属性,后面形参
}
public Person(String name,int age){
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; //this可以理解为当前对象(类似“对象.属性”)。用来区分前面是属性,后面是形参
}
public int getAge(){
return age;
}
}
this修饰构造器(开发中经常用):
①我们在类的构造器中,可以显式使用 “this(形参列表)” 方式,调用本类中指定的其他构造器;
②构造器中不能通过 “this(形参列表)” 方式调用自己;
③如果一个类中有n个构造器,则最多有(n-1)个构造器中使用了 “this(形参列表)” ;
④规定:“this(形参列表)” 必须声明在当前构造器的首行;
⑤构造器内部,最多只能声明一个 “this(形参列表)” ,用来调用其他构造器。
练习:
①Boy.java
package com.atguigu.exer;
public class Boy {
private String name;
private int age;
//alt+shift+s:生成构造器(可手动选择参数个数)
public Boy() {
}
public Boy(String name, int age) {
super();
this.name = name;
this.age = age;
}
//alt+shift+s:生成get和set方法
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;
}
//marry方法
public void marry(Girl girl){
System.out.println("我想娶"+girl.getName());
}
public void shout(){
if(age>=22){
System.out.println("你可以合法登记结婚了!");
}else{
System.out.println("年龄不合要求!");
}
}
}
②Girl.java
package com.atguigu.exer;
public class Girl {
private String name;
private int age;
//构造器(空参)
public Girl() {
}
//构造器(两个参数)
public Girl(String name, int age) {
this.name = name;
this.age = age;
}
//name的get和set方法
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); //this表示当前对象,谁调用marry(),谁就是this,注意不要写girl.name因为传入的应该是Girl类型
}
/**
*
* @Description 比较两个对象的大小
* @author hzb
* @date 2022年3月16日下午4:51:36
* @param girl
* @return 返回正数:当前对象大;返回负数:当前对象小;返回0:当前对象与形参对象相等
*/
public int compare(Girl girl){
if(this.age>girl.age){ //调用方法的那个对象的属性和形参对象的属性对比
return 1;
}else if(this.age<girl.age){
return -1;
}else{
return 0;
}
}
}
③BoyGirlTest.java
package com.atguigu.exer;
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("祝英台",19);
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(girl.getName()+"和"+girl1.getName()+"一样大");
}
}
}
实验一
1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:
账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和 setter方法),取款方法 withdraw(),存款方法 deposit()。
package com.atguigu.exer;
public class Account {
private int id; //账号
private double balance; //余额
private double annualInterestRate; //年利率
//构造器
public Account (int id, double balance, double annualInterestRate ){
this.id=id;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
}
//三个属性的get和set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//存取款方法
public void withdraw (double amount){ //存钱
if(amount>0){ //合法时执行
balance+=amount;
System.out.println("成功存入:"+amount+"元");
}
}
//在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
public void deposit (double amount){ //取钱
if(balance<amount){
System.out.println("余额不足,取款失败");
return; //结束方法
}
balance-=amount;
System.out.println("成功取出:"+amount+"元");
}
}
- 创建 Customer 类。
package com.atguigu.exer;
public class Customer {
private String firstName;
private String lastName;
private Account account; //一个类的属性中出现了自定义类是可行的
//构造器
public Customer(String f,String l){
firstName=f; //没有重名加不加this都可以
lastName=l;
}
//写入题目要求的get和set方法
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
3.写一个测试程序。
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元,年利率为 1.23% 的账户。
(2) 对 Jane Smith 操作。存入 100 元,再取出 960 元。再取出 2000 元。打印出 Jane Smith 的基本信息
package com.atguigu.exer;
public class CustomerTest {
public static void main(String[] args) {
Customer cust=new Customer("Jane","Smith");
Account acct=new Account(1000,2000,0.0123);
cust.setAccount(acct);
cust.getAccount().deposit(100); //存钱的时候先找到账户,账户有存取钱方法
cust.getAccount().withdraw(960);
cust.getAccount().withdraw(2000);
System.out.println("Customer ["+cust.getLastName()+","+cust.getFirstName()+
"] has a account: id is "+cust.getAccount().getId()+", annualInterestRate is "+
cust.getAccount().getAnnualInterestRate()*100+"%"+", balance is "+cust.getAccount().getBalance());
}
}
实验二(对象数组)
package com.atguigu.java2;
public class Account {
private double balance;
//构造器
public Account(double init_balance){
this.banlance=init_balance;
}
//方法
public double getBalance(){
return balance;
}
public void deposit(double amt){ //存钱
if(amt>0){
balance+=amt;
System.out.println("存款成功");
}
return;
}
//取钱操作
public void withdraw(double amt){
if(amt<=balance){
balance-=amt;
System.out.println("取款成功");
return;
}
System.out.println("取款失败");
}
}
package com.atguigu.java2;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f,String l){
firstName=f;
lastName=l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
①addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer 对象,然后把它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。
②getNumOfCustomers 方法返回 numberofCustomers 属性值。
③getCustomer 方法返回与给出的 index 参数相关的客户。
4. 创建 BankTest 类,进行测试
package com.atguigu.java2;
public class BankTest {
public static void main(String[] args) {
Bank bank=new Bank();
bank.addCustomer("Jane", "Smith"); //添加之后此人还没有账户
bank.getCustomer(0).setAccount(new Account(2000));//先找到这个人,再给他set一个账户(匿名对象方式)
bank.getCustomer(0).getAccount().withdraw(500); //取钱
double money=bank.getCustomer(0).getAccount().getBalance(); //余额
System.out.println("客户"+bank.getCustomer(0).getFirstName()+"的余额为:"+money);
}
}
package、import关键字的使用
package关键字的使用
- 1.为了更好地实现项目中类的管理,提供包的概念
- 2,使用package声明类或接口所属的包,声明在源文件开头
- 3,包名(小写)遵循标识符命名规则,要见名知意
- 4,每“.”一次,就代表一层文件目录
- 5,同一个包下,不能命名同名的接口或类。不同包下可以
import关键字的使用
- 1,在源文件中显式的使用import结构导入指定包下的类、接口。
- 2,声明在包的声明和类的声明之间
- 3,如果需要导入多个结构时,并列写出
- 4,可以使用xxx.*的格式表示导入xxx包下的所有结构,但如果使用的是xxx子包下的结构,则仍需要显式导入
- 5,如果使用的类或接口是java.lang包下定义的,则可以省略import结构
- 6,如果使用的类或接口是本包下定义的,则也可以省略import结构
- 7,如果在原文件下,使用了不同包下的同名的类,则必须至少有一个类需要以全类名的方式显示