Java-04面向对象(上)

一、类与对象

1、类和对象是面向对象的2个基本概念,类和对象区别:对象是抽象的,类是一个个实体,是对象的一个实例

  类最重要的是成员变量(属性),成员方法(行为)

2、标准类如何定义

3、对象的内存解析

4、变量

可变个数形参数

5、方法

5.1、重载:【方法名一样,参数个数和参数类型不同即可】

方法重载辨别原则:只能唯一一个方法适用,不出现歧义

判 断:与void show(int a,char b,double c){}构成重载的有:
a) void show(int x,char y,double z){} // no
b) int show(int a,double c,char b){} // yes
c) void show(int a,double c,char b){} // yes 
d) boolean show(int c,char b){}
// yes e) void show(double c){} // yes f) double show(int x,char y,double z){} // no
g) void shows(){double c}
// no

5.2、可变参数

public void method(int a, double d,String... strs){}

5.3、值传递

方法如果有参数,那么定义在方法头部的参数叫形参,传入的值叫实参

Java都是值传递,基本数据类型是值,引用数据类型是地址值

值传递坑题目

int[] arr =new int[]{2,3}

sout(arr)地址值

char[] arr =new char[]{'a','b','c'}

sout(arr)abc,不是地址值

(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个 findArea()方法返回圆的面积。

(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义 如下:public void printAreas(Circle c, int time)

在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积。 例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。

(3)在main方法中调用printAreas()方法,调 用完毕后输出当前半径值。程序运行结果如图 所示。 

考察值传递以及自定义数据类型,对象作为参数
public
class TestArea { public static void main(String[] args) { PassObject d = new PassObject(){}; Circle c = new Circle(){}; int time =5; d.printAreas( c, time); System.out.println("now radius is"+"\t"+(c.radius+1)); } } //定义circle类 public class Circle { public double radius; public void findArea(){ System.out.println(Math.PI * radius *radius); } } //定义PassObject类 class PassObject { public void printAreas(Circle c, int time){ System.out.println("Radius"+"\t"+"Areas"); for (int i = 1; i <= time; i++) { System.out.print(i+"\t"); c.radius=i; c.findArea(); } } }

 5.4、递归

一个方法内循环调用,但是有方向的,终止的,否则是死循环

//    递归练习1、求1-100自然数和
    public static int sum (int num){
        if (num==1){
            return 1;
        }
        else {
            return num+sum(num-1);
        }
    }
//    递归练习2、求n!
    public static int result(int n){
        if(n==1){
            return 1;
        }
        else{
            return n*result(n-1);
        }
    }
//    递归练习3、f(0)=1,f(1)=4,f(n+2)=2f(n+1)+f(n),求f(10)
    public static int f(int n){
        if(n==0){
            return 1;
        }
        else if(n==1){
            return 4;
        }else {
            return 2*f(n-1)+f(n-2);
        }
    }
//    递归练习4、f(20)=1,f(21)=4,f(n+2)=2f(n+1)+f(n),求f(10)
public static int f(int n){
if(n==20){
return 1;
}
else if(n==21){
return 4;
}else {
return f(n+2)-2*f(n+1);
}
}


// 递归练习5、计算斐波那契数列(Fibonacci)的第n个值 1 1 2 3 5 8 13 21 34 55。。。并打印输出数列

主方法内: {

        System.out.println(f(10));
        for (int i = 1; i <11 ; i++) {
            System.out.print(f(i)+"\t");
      }

      public static int f(int n){
      if(n==1){
      return 1;
       }
       else if(n==2){
       return 1;
       }else {
      return f(n-1)+f(n-2);
      }
      }

 二、面向对象特征

我们程序设计追求“高内聚,低耦合”。
高内聚 :类的内部数据操作细节自己完成,不允许外部干涉; 低耦合 :仅对外暴露少量的方法用于使用。

隐藏对象内部的复杂性,只对外公开简单的接口。便于外界调用,从而提高系统的可扩展性、可维护性。通俗的说,把该隐藏的隐藏,该暴露的暴露。这就是封装性的设计思想。 

1、修饰符

封装体现需要修饰符配合 

 

2、构造器

如果没写构造器,系统会赠送无参构造器;如果写了,系统就不赠送了

构造器目的:为了创建对象+初始化对象

3、this

this调用属性、行为、构造器

this调用构造器(构造器之间调用,1、不能循环调用,2、必须第一条语句)

默认构造器和类的修饰权限一致

试验1

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

2. 创建 Customer 类。 

3.写一个测试程序。
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元,

年利率为 1.23% 的账户。

(2) 对 Jane Smith 操作。

存入 100 元,再取出 960 元。再取出 2000 元。 打印出 Jane Smith 的基本信息 

//3、测试类
public class Test {
    public static void main(String[] args) {
        //个人信息
        Customer customer = new Customer("Smith","Jane"){};
        //账号信息
        Account account = new Account(1000,2000,0.0123){};
        //获取账户
        customer.setAccount(account);//把账号设置进去
        //存取行为
//        account.deposit(100);
//        account.withdraw(960);
//        account.withdraw(200);
        //customer设置了account账号,间接使用对象.方法[account.deposit(amount)]
        customer.getAccount().deposit(100);
        customer.getAccount().withdraw(960);
        customer.getAccount().withdraw(200);
        customer.showCustomer();
    }
}

//1、创建账户类,实现存钱功能
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(){
        this.balance = balance;
    }
    public double getAnnualinterestRate(){
        return annualinterestRate;
    }
    public void setAnnualinterestRate(){
        this.annualinterestRate = annualinterestRate;
    }
    public void withdraw(double amount){//取钱方法
        if(amount>balance){
            System.out.println("余额不足,取钱失败!最多只能取"+balance+""+"\n"+"请重试");
            return;//取钱操作失败,结束方法,重新调用方法并输入合适的值才能有后面操作
        }
        System.out.println("成功取出"+amount);
        balance-=amount;//更新余额
       
    }
    public void deposit(double amount){//存钱方法
        System.out.println("成功存入"+amount);
        balance+=amount;//更新余额
    }

}
//2、创建用户类
public class Customer {
    private String firstName;
    private String lastName;
    private Account account;//自定义数据类型,实现某种功能,这就是封装的体现,开发中经常这么操作
    public Customer(String firstName,String lastName){
        this.firstName=firstName;
        this.lastName=lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;//获取账户对象
    }

    public void setAccount(Account account) {
        this.account = account;
    }
    public void showCustomer( ){//输出客户信息
        System.out.println("姓名:"+lastName+"\t"+firstName);
    
System.out.println("账户:"+account.getId()+"\t余额:"+account.getBalance()+"\t存款利率"+account.getAnnualinterestRate());
  } 
}

试验2

 相比试验1,增加bank类用来存储多个customer

//4、测试类
public class Test {
    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.addCustomer("Jane","Smith");
        bank.addCustomer("Alicia","key");
        bank.addCustomer("jude","low");
//客户Alicia key存取操作
        Customer one = bank.getCustomer(1);
        Account account = new Account(3000);//初始余额
        one.setAccount(account);
        one.getAccount().withdraw(960);//取钱
        one.getAccount().deposit(200);//存钱
        one.showCustomer();

    }
}
//1、创建账户类,实现存钱功能
public class Account {
    private double balance;

    public Account(double init_balance) {
        this.balance = init_balance;
    }
    public double getBalance() {
        return balance;
    }
    public void withdraw(double amt) {//取钱方法
        if (amt > balance) {
            System.out.println("余额不足,取钱失败!最多只能取" + balance + "" + "\n" + "请重试");
            return;//取钱操作失败,结束方法,重新调用方法并输入合适的值才能有后面操作
        } else {
            System.out.println("成功取出" + amt);
            balance -= amt;//更新余额
        }
    }

    public void deposit(double amount) {//存钱方法
        System.out.println("成功存入" + amount);
        balance += amount;//更新余额
    }
}
//2、创建用户类
public class Customer {
    private String firstName;
    private String lastName;
    private Account account;//自定义数据类型,实现某种功能,这就是封装的体现,开发中经常这么操作
    public Customer(String firstName,String lastName){
        this.firstName=firstName;
        this.lastName=lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;//获取账户对象
    }

    public void setAccount(Account account) {
        this.account = account;
    }
    public void showCustomer( ){//输出客户信息
        System.out.println("姓名:"+firstName+"\t"+lastName);
        System.out.println("余额:"+account.getBalance());
    }
}
//3、创建银行,将用数组装【用户对象】
public class Bank {
    private Customer[] customers;
    private int numberOfCustomers;//记录客户个数
    public Bank(){
        customers=new Customer[10];//初始化数组
    }
    //    添加客户
    public void addCustomer(String f,String l){
        Customer cust=new Customer(f,l){};//创建对象
//        customers=new Customer[10];//不能在这里,这样会导致每次new了一个bank就产生一个数组,初始化应该在本方法外面
        customers[numberOfCustomers]=cust;//对象塞进数组
        numberOfCustomers++;
    }
    public int getNumberOfCustomers(){
        return numberOfCustomers;
    }

    public Customer getCustomer(int index) {
        if(index>=numberOfCustomers){
            System.out.println("该客户不存在!");
            return null;
        }
        return customers[index];//返回客户
    }
}

 

posted @ 2021-08-22 01:32  我也不知道怎么起昵称  阅读(76)  评论(0)    收藏  举报