构造器练习

 练习一

题目

案例:
(1)定义Student类,有4个属性:
  String name;
  int age;
  String school;
  String major;

(2)定义Student类的3个构造器:

- 第一个构造器Student(String n, int a)设置类的name和age属性;
- 第二个构造器Student(String n, int a, String s)设置类的name, age 和school属性;
- 第三个构造器Student(String n, int a, String s, String m)设置类的name, age ,school和major属性;

(3)在main方法中分别调用不同的构造器创建的对象,并输出其属性值。

代码

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer1;


public class Student {

    String name;
    int age;
    String school;
    String major;//专业

    public Student(String n,int a){
        name = n;
        age = a;
    }
    public Student(String n, int a, String s){
        name = n;
        age = a;
        school = s;
    }

    Student(String n, int a, String s, String m){
        name = n;
        age = a;
        school = s;
        major = m;
    }

    public String getInfo(){
        return "name = " + name + ",age = " + age +
                ",school = " + school + ", major = " + major;
    }

}

 

 

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer1;

/**
 * ClassName: StudentTest
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 16:45
 * @Version 1.0
 */
public class StudentTest {
    public static void main(String[] args) {

        Student s1 = new Student("刘强东",48,"中国人民大学","社会学");
        System.out.println(s1.getInfo());

        Student s2 = new Student("奶茶妹妹",28,"清华大学");
        System.out.println(s2.getInfo());
    }
}

 

 

练习二

题目

案例: 编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height, 同时声明公共方法访问私有变量。此外,提供类必要的构造器。另一个类中使用这些公共方法,计算三角形的面积。

代码

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer2;

/**
 * ClassName: TriAngle
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 16:49
 * @Version 1.0
 */
public class TriAngle {
    //属性
    private double base;//底边长
    private double height;//高

    public double getBase(){
        return base;
    }
    public void setBase(double b){
        base = b;
    }
    public double getHeight(){
        return height;
    }
    public void setHeight(double h){
        height = h;
    }

    public TriAngle(){

    }
    public TriAngle(double b,double h){
        base = b;
        height = h;
    }
    //...

    //求面积的方法
    public double findArea(){
        return base * height / 2;
    }

}

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer2;

/**
 * ClassName: TriAngleTest
 * Description:
 *
 * @Author 尚硅谷-宋红康
 * @Create 16:52
 * @Version 1.0
 */
public class TriAngleTest {
    public static void main(String[] args) {
        //创建TriAngle的实例1
        TriAngle t1 = new TriAngle();

        t1.setHeight(2.3);
        t1.setBase(3.4);

        System.out.println("面积为:" + t1.findArea());

        //创建TriAngle的实例2
        TriAngle t2 = new TriAngle(2.4,4.5);

        System.out.println("底边长为:" + t2.getBase());
        System.out.println("高为:" + t2.getHeight());
        System.out.println("面积为:" + t2.findArea());
    }
}

 

 练习三

题目

案例:

1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。
该类包括的属性:账号id,余额balance,年利率annualInterestRate;
包含的构造器:自定义
包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。

提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。


2、创建Customer类。
a. 声明三个私有对象属性:firstName、lastName和account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)
c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。
d. 声明setAccount 方法来对account属性赋值。
e. 声明getAccount 方法以获取account属性。

3、写一个测试程序。

(1)创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
(2)对Jane Smith操作。
存入 100 元,再取出960元。再取出2000元。
打印出Jane Smith 的基本信息:

成功存入 :100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0

代码

Account

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer3;

public class Account {
    private int id;//账号
    private double balance; //余额
    private double annualInterestRate;//年利率

    public Account(int i, double b, double a) {
        id = i;
        balance = b;
        annualInterestRate = a;
    }

    public void setId(int i) {
        id = i;
    }

    public int getId() {
        return id;
    }

    public void setBalance(double b) {
        balance = b;
    }

    public double getBalance() {
        return balance;
    }

    public void setAnnualInterestRate(double a) {
        annualInterestRate = a;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    //取钱
    public void withdraw(double amount){
        if(amount <= balance && amount > 0){
            balance -= amount;
            System.out.println("成功取出:" + amount);
        }else{
            System.out.println("余额不足,取款失败");
        }

    }
    //存款
    public void deposit(double amount){
        if(amount > 0){
            balance += amount;
            System.out.println("成功存入:" + amount);
        }
    }


}

Customer

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer3;


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 void setAccount(Account a){
        account = a;
    }
    public Account getAccount(){
        return account;
    }

}

package chapter06_oop1_teacher.src.com.atguigu08.constructor.exer3;

public class CustomerTest {
    public static void main(String[] args) {
        //创建Customer实例
        Customer customer = new Customer("Jane","Smith");

//        Account account = new Account(1000,2000,0.0123);
//        customer.setAccount(account);
        //或
        customer.setAccount(new Account(1000,2000,0.0123));



        //针对于客户的账户进行取钱、存钱的操作
        customer.getAccount().deposit(100);
        customer.getAccount().withdraw(960);
        customer.getAccount().withdraw(2000);

        //输出客户信息
        //Customer [Smith, Jane] has a account: id is 1000,
        // annualInterestRate is 1.23%, balance is 1140.0
        System.out.println("Customer [" + customer.getLastName() + "," + customer.getFirstName() +
                "] has a account:id is " + customer.getAccount().getId() + ",annualInterestRate is " +
                customer.getAccount().getAnnualInterestRate()*100 + "%,balance is " +
                customer.getAccount().getBalance());


        /*
         * 关于匿名对象
         *
         * 1. 匿名对象往往只能被调用一次
         * 2. 匿名对象常常作为实参传递给方法的形参。
         * */
        new Account(1001,2000,0.0123).withdraw(1000);
        System.out.println(new Account(1001, 2000, 0.0123).getBalance());

        int num = 10;


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值