this关键字的使用
package seven;
/**
* this:
* 1.可以用来修饰属性、方法、构造器
* 2.this 理解为当前对象或当前正在创建的对象。比如:this.name,this.show();
* 3.可以在构造器中通过「this(形参)」的方式显式的调用本类中其它重载的指定的构造器。
* 要求:1.在构造器内部必须声明在首行!
* 2.若一个类中有n个构造器,那么最多有 n-1 个构造器中使用了 this(形参);
*/
public class TestPerson {
public static void main(String[] args) {
Person p = new Person();
p.setAge(20);
p.info();
p.setName("奥斯卡");
p.info();
System.out.println();
Person p1 = new Person("马红俊", 11);
p1.info();
Person p2 = new Person(22);
p2.info();
}
}
class Person{
private String name;
private int age;
public Person(){
name = "三哥";
age = 14;
}
public Person(String name){
this(15);
System.out.println("这是Person类中形参为name的构造器");
this.name = name;
}
public Person(int age){
this();
this.age = age;
}
//this.name :表示当前正在创建的对象
//name:是形参
public Person(String name, int age){
// this.name = name;
//this():可以用来显式的调用当前类的重载的指定的构造器(根据形参判定)
this(name);
System.out.println("这是Person类中形参为name和age的构造器");
this.age = age;
}
// public void setName(String n){
// name = n;
// }
//this.name :表示当前对象的属性
//name:是形参
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 sleep(){
System.out.println("就寝");
}
public void info(){
System.out.println("name:" + this.name + "age:" + this.age);
this.show();
}
public void show(){
System.out.println("The Chinese culture");
}
}
package seven;
/**
* 4.编写两个类,TriAngle和TestTriAngle,其中TriAngle中声明私有的底边长base和高height,
* 同时声明公共方法访问私有变量;另一个类中使用这些公共方法,计算三角形的面积。
*/
public class TestTriAngle { //Angle :角
public static void main(String[] args) {
TriAngle t = new TriAngle();
t.setBase(2.3);
t.setHeight(1.2);
System.out.println("面积为:" + t.findArea());
}
}
class TriAngle{
private double base;//底边长
private double height;//高
public TriAngle(){
this.base = 1.0;
this.height = 1.0;
}
public TriAngle(double base,double height){
this.base = base;
this.height = height;
}
public double getBase(){
return base;
}
public double getHeight(){
return height;
}
public void setBase(double base){
this.base = base;
}
public void setHeight(double height){
this.height = height;
}
public double findArea(){
return this.base * this.height / 2;
}
}
this关键字的使用练习一
代码一
package seven;
/**
* 1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,
年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。
提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
*/
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;
}
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(balance >= amount){
balance -= amount;
System.out.println("成功取出: " + amount);
}else {
System.out.println("余额不足,取款失败");
}
}
//存钱
public void deposit (double amount){
balance += amount;
System.out.println("成功存入:" + amount);
}
}
代码二
package seven;
/**
* a. 声明三个私有对象属性:firstName、lastName和account。
* b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)
* c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。
* d. 声明setAccount 方法来对account属性赋值。
* e. 声明getAccount 方法以获取account属性。
*/
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;
}
}
代码三
package seven;
/**
* 3.写一个测试程序。
* (1) 创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
* (2) 对Jane Smith操作。
* 存入 100 元,再取出960元。再取出2000元。
* 打印出Jane Smith 的基本信息
*/
public class TestCustomer {
public static void main(String[] args) {
Customer cust = new Customer("Jane", "Smith");
cust.setAccount(new Account(100,2000,0.0123));
Account account = cust.getAccount();
account.deposit(100);
account.withdraw(960);
account.withdraw(2000);
System.out.println("Customer [" + cust.getLastName() + "," + cust.getFirstName() + "] hao a account" + account.getId()
+ ",annualInterestRate" + account.getAnnualInterestRate()*100 + "%,balance is" + account.getBalance());
}
}
this关键字的使用练习二
代码一
package seven;
public class Boy {
private String name;
private int 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 seven;
public class Girl {
private String name;
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);
}
}
代码三
package seven;
public class TestBoyGirl {
public static void main(String[] args) {
Boy boy = new Boy();
boy.setName("徐三石");
boy.setAge(20);
Girl girl = new Girl();
girl.setName("江楠楠");
boy.marry(girl);
boy.shout();
System.out.println();
girl.marry(boy);
}
}
韧桂 2019-12-25