30天搞定Java--day12

每日一考和复习

每日一考
在这里插入图片描述

package com.water.project02;

public class CustomerList {
	
	private Customer[] customers;
	private int total;            // 现有客户数量
	
	public CustomerList(int totalCustomer) {
		this.customers = new Customer[totalCustomer];
	}
	
	/**
	 * 
	 * @Description 添加指定的客户到数组中
	 * @author water
	 * @date 2020年3月31日下午4:32:03
	 * @param customer
	 * @return 添加是否成功
	 */
	public boolean addCustomer(Customer customer) {
		if (total >= this.customers.length) {
			return false;
		}
		this.customers[this.total] = customer;
		this.total += 1;
		return true;
	}
	
	/**
	 * 
	 * @Description 替换指定索引位置上的数组元素
	 * @author water
	 * @date 2020年3月31日下午4:32:39
	 * @param index
	 * @param customer
	 * @return 是否替代成功
	 */
	public boolean replaceCustomer(int index,Customer customer) {
		if (index >= total || index < 0){  
			return false;
		}
		this.customers[index] = customer;
		return true;
	}
	
	/**
	 * 
	 * @Description 删除指定索引上的元素
	 * @author water
	 * @date 2020年3月31日下午4:33:25
	 * @param index
	 * @return 是否删除成功
	 */
	public boolean daleteCustomer(int index) {
		if (index >= total || index < 0){
			return false;
		}
		for(;index < this.total;index++) {
			this.customers[index] = this.customers[index + 1];
		}
		this.total -= 1;
		return true;
	}
	
	/**
	 * 
	 * @Description 获取所有的customers对象构成的数组
	 * @author water
	 * @date 2020年3月31日下午4:34:08
	 * @return
	 */
	public Customer[] getAllCustomers() {
		Customer[] customersTotal = new Customer[this.total];
		for(int i = 0;i<this.total;i++) {
			customersTotal[i] = this.customers[i];
		}
		return customersTotal;
	}
	
	/**
	 * 
	 * @Description 返回指定索引位置上的Customer
	 * @author water
	 * @date 2020年3月31日下午4:34:39
	 * @param index
	 * @return 不存在则返回null
	 */
	public Customer getCustomer(int index) {
		if (index >= total || index < 0){
			return null;
		}
		return customers[index];
	}
	
	/**
	 * 
	 * @Description 返回Customer对象的个数
	 * @author water
	 * @date 2020年3月31日下午4:35:25
	 * @return
	 */
	public int getTotal() {
		return this.total;
	}
}

复习

day11的学习内容

面向对象

面向对象特征之二:继承性(接day11)

继承性练习

(1)定义一个ManKind类,包括

  • 成员变量int sex和int salary;
  • 方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
  • 方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)

(2)定义类Kids继承ManKind,并包括

  • 成员变量int yearsOld;
  • 方法printAge()打印yearsOld的值

(3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问
其父类的成员变量及方法

public class ManKind {

	private int sex;// 性别
	private int salary;// 薪资

	public ManKind() {
	}

	public ManKind(int sex, int salary) {
		this.sex = sex;
		this.salary = salary;
	}

	public void manOrWoman() {
		if (sex == 1) {
			System.out.println("man");
		} else if (sex == 0) {
			System.out.println("woman");
		}
	}

	public void employeed() {
		if(salary == 0){
			System.out.println("no job");
		}else{
			System.out.println("job");
		}
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
}
public class Kids extends ManKind{
	private int yearsOld;
	
	public Kids() {
	}

	public Kids(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public void printAge(){
		System.out.println("I am " + yearsOld + " years old.");
		
	}

	public int getYearsOld() {
		return yearsOld;
	}

	public void setYearsOld(int yearsOld) {
		this.yearsOld = yearsOld;
	}
}
public class KidsTest {
	public static void main(String[] args) {

		Kids someKid = new Kids(12);

		someKid.printAge();

		someKid.setSalary(0);
		someKid.setSex(1);

		someKid.employeed();
		someKid.manOrWoman();
	}
}

方法的重写

  • 定义:在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法

  • 要求:

    1. 子类重写的方法必须和父类被重写的方法具有相同的方法名称、参数列表
    2. 子类重写的方法的返回值类型不能大于父类被重写的方法的返回值类型
    3. 子类重写的方法使用的访问权限不能小于父类被重写的方法的访问权限(子类的返回值类型必须是父类返回值类型或者其子类,若为基本数据类型必须相同)
      子类不能重写父类中声明为private权限的方法
    4. 子类方法抛出的异常不能大于父类被重写方法的异常
  • 注意
    子类与父类中同名同参数的方法必须同时声明为非static的(即为重写),或者同时声明为
    static的(不是重写)。因为static方法是属于类的,子类无法覆盖父类的方法

四种访问权限修饰符

Java权限修饰符public、protected、(缺省)、private置于类的成员(属性、方法、构造器、内部类)定义前,用来限定对象对该类成员的访问权限

修饰符 类内部 同一个包 不同包的子类 同一个工程
private Yes
缺省 Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes

不同包的子类:不在同一个包,但存在继承关系
对于class的权限修饰只可以用public和default(缺省)

  • public类可以在任意地方被访问
  • default类只可以被同一个包内部的类访问

关键字:super

  • 在Java类中使用super<>/font来调用父类中的指定操作:

    • super可用于访问父类中定义的属性
    • super可用于调用父类中定义的成员方法
    • super可用于在子类构造器中调用父类的构造器
  • 注意:

    • 尤其当子父类出现同名成员时,可以用super表明调用的是父类中的成员
    • super的追溯不仅限于直接父类,优先从直接父类向上找
    • super和this的用法相像,this代表本类对象的引用,super代表父类的内存空间的标识

使用举例

class protected Person {
	String name = "张三";
	protected int age;

	public String getInfo() {
		return "Name: " + name + "\nage: " + age;
	}
}

class Student extends Person {
	protected String name = "李四";
	private String school = "New Oriental";

	public String getSchool() {
		return school;
	}

	public String getInfo() {
		return super.getInfo() + "\nschool: " + school;
	}
}

public class StudentTest {
	public static void main(String[] args) {
		Student st = new Student();
		System.out.println(st.getInfo());
	}
}

调用父类的构造器

  • 子类中所有的构造器默认都会访问父类中空参数的构造器
  • 当父类中没有空参数的构造器时,子类的构造器必须通过this(参数列表)或者super(参数列表)语句指定调用本类或者父类中相应的构造器。同时,只能”二选一”,且必须放在构造器的首行
  • 如果子类构造器中既未显式调用父类或本类的构造器,且父类中又没有无参的构造器,则编译出错
  • 在类的多个构造器中,至少有一个构造器使用了super(形参列表),调用父类中的构造器

实验:类的继承,super

题目要求

package com.water.test;

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 double getMonthlyInterest() {
		return this.annualInterestRate / 12;
	}

	/**
	 * 
	 * @Description 取钱
	 * @author water
	 * @date 2020年3月31日下午10:22:20
	 * @param amount
	 */
	public void withdraw(double amount) {
		if (amount > this.balance) {
			System.out.println("余额不足");
		} else {
			this.balance -= amount;
			System.out.println("您的账户余额:"+this.balance);
		}
	}

	/**
	 * 
	 * @Description 存钱
	 * @author water
	 * @date 2020年3月31日下午10:22:42
	 * @param amount
	 */
	public void deposit(double amount) {
		if (amount <= 0) {
			System.out.println("输入错误");
		} else {
			this.balance += amount;
			System.out.println("您的账户余额:"+this.balance);
		}
	}
}
package com.water.test;

public class CheckAccount extends Account{
	
	private double overdraft;
	
	public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
		super(id, balance, annualInterestRate);
		this.overdraft = overdraft;
	}
	
	@Override
	public void withdraw(double amount) {
		if (amount > this.getBalance() + this.overdraft) {
			System.out.println("超过可透支限额!");
		} else if(amount < this.getBalance()) {
			this.setBalance(this.getBalance() - amount);
			System.out.println("您的账户余额:"+this.getBalance());
			System.out.println("您的可透余额:"+this.overdraft);
		}else {
			this.overdraft -= (amount - this.getBalance());
			this.setBalance(0);
			System.out.println("您的账户余额:"+this.getBalance());
			System.out.println("您的可透余额:"+this.overdraft);
		}
	}
}
package com.water.test;

public class AccountTest {
	public static void main(String[] args) {
		Account account = new Account(1122,20000,0.045);
		
		CheckAccount checkAccount = new CheckAccount(1122, 20000, 0.045, 5000);
		
		account.withdraw(30000);
		account.withdraw(2500);
		account.deposit(3000);
		System.out.println("月利率:" + account.getMonthlyInterest());
		
		System.out.println("************************");
		
		checkAccount.withdraw(5000);
		checkAccount.withdraw(18000);
		checkAccount.withdraw(3000);
	}
}
余额不足
您的账户余额:17500.0
您的账户余额:20500.0
月利率:0.00375
************************
您的账户余额:15000.0
您的可透余额:5000.0
您的账户余额:0.0
您的可透余额:2000.0
超过可透支限额!

面向对象特征之三:多态性

  1. 何为多态性:
    多态性,是面向对象中最重要的概念,在Java中的体现:
    对象的多态性:父类的引用指向子类的对象
    可以直接应用在抽象类和接口上

  2. 多态的使用:虚拟方法调用
    有了对象的多态性以后,我们在编译期,只能调用父类中声明的方法,但在运行期,我们实际执行的是子类重写父类的方法
    总结:编译,看左边;运行,看右边

  3. 多态性的使用前提: ① 类的继承关系 ② 方法的重写

  4. 对象的多态性,只适用于方法,不适用于属性(编译和运行都看左边)

  5. 为什么要有多态?
    提高了代码的通用性,常称作接口重用

发布了20 篇原创文章 · 获赞 33 · 访问量 6199

猜你喜欢

转载自blog.csdn.net/weixin_42224119/article/details/105225517