Java三大特性(例题及代码)

**

1、封装,将对象作为参数传递给方法。

**
题目要求:
(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,以及对应的圆面积。
在main方法中调用printAreas()方法,调用完毕后输出当前半径值
程序运行结果如图所示
在这里插入图片描述

public class Circle {
	private double radius;

	public Circle() {
		super();
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double findArea() {
		double area = radius * radius * Math.PI;
		return area;
	}
}
public class PassObject {
	public void printAreas(Circle c, int time) {
		System.out.println("Radius\t" + "Area");
		for (int i = 1; i <= time; i++) {
			c.setRadius(i);
			System.out.println(i + "\t" + c.findArea());
		}
	}
}
public class Test {
	public static void main(String[] args) {
		PassObject p = new PassObject();
		Circle c = new Circle();
		p.printAreas(c, 5);
	}
}

**

2.类的继承,super

**
(1)写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
(2)写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。

提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
运行结果如图所示:
在这里插入图片描述

public class Account {
	private int id;
	private double balance;
	private double annualInterestRate;

	public Account() {
		super();
	}

	public Account(int id, double balance, double annualInterestRate) {
		super();
		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 annualInterestRate / 12;
	}

	public void withdraw(double amount) {
		if (amount > balance) {
			System.out.println("余额不足!\n" + "您的账户余额为" + balance);
		} else {
			balance -= amount;
			System.out.println("您的账户余额为:" + balance + "\n" + "月利率为" + getMonthlyInterest() * 100 + "%");
		}
	}

	public void deposit(double amount) {
		if (amount > 0) {
			balance += amount;
			System.out.println("您的账户余额为:" + balance + "\n" + "月利率为" + getMonthlyInterest() * 100 + "%");
		} else
			System.out.println("不能是0或负");
	}
}
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;
	}

	public CheckAccount() {
		super();
	}

	public void withdraw(double amount) {
		double balance = super.getBalance();
		if (amount <= super.getBalance()) {
			balance = balance - amount;
			super.setBalance(balance);
			System.out.println("您的账户余额为:" + super.getBalance() + "\n" + "您的可透支余额:" + overdraft);
		} else {
			if (amount > (balance + overdraft)) {
				System.out.println("超过可透支限额");
			} else {
				overdraft -= (amount - balance);
				balance = 0;
				super.setBalance(balance);
				System.out.println("您的账户余额为:" + super.getBalance() + "\n" + "您的可透支余额:" + overdraft);
			}
		}
	}
}
public class Test {
	public static void main(String[] args) {
		Account acct = new Account(1122, 20000, 0.045);
		acct.withdraw(30000);
		acct.withdraw(2500);
		acct.deposit(3000);

		CheckAccount checkAcct = new CheckAccount(1122, 20000, 0.045, 5000);
		checkAcct.withdraw(5000);
		checkAcct.withdraw(18000);
		checkAcct.withdraw(3000);
	}
}

**

3.多态

**
父类 Vehicle 有 run 方法,子类都重写了 run ,Bicycle 自行车 ,Car 汽车,Bus 公交车。
一个学生Student 回家坐何种交通工具?学生类里有个goHome方法(某种类型的交通工具)。
在测试类里具体运行:
在这里插入图片描述

public class Vehicle {
	public void run() {
		System.out.println("回家");
	}
}

class Bicycle extends Vehicle {
	public void run() {
		System.out.println("骑自行车回家");
	}
}

class Car extends Vehicle {
	public void run() {
		System.out.println("打车回家");
	}
}

class Bus extends Vehicle {
	public void run() {
		System.out.println("坐公交回家");
	}
}
class Student {
	public void goHome(Vehicle v) {
		v.run();
	}

	public static void main(String[] args) {
		Student s = new Student();
		Vehicle v1 = new Bicycle();
		Vehicle v2 = new Car();
		Vehicle v3 = new Bus();
		s.goHome(v1);
		s.goHome(v2);
		s.goHome(v3);
	}
}

**

4.多态(向下转型(拆箱))

**
将父类引用中的真实子类对象,强转回子类本身类型,称为向下转型。
只有转换回子类真实类型,才可调用子类独有的属性和方法。
在这里插入图片描述

public class Animal {
	private String name;
	private String sex;

	public void printInfo() {
		System.out.println("动物");
	}
}
public class Cat extends Animal {
	String name = "英短";
	String sex = "母";

	public void printInfo() {
		System.out.println(sex + "的" + name);
	}
}
public class Dog extends Animal {
	String name = "哈士奇";
	String sex = "公";

	public void printInfo() {
		System.out.println(sex + "的" + name);
	}
}
public class Test {

	public static void main(String[] args) {
		Dog dog = new Dog();
		Cat cat = new Cat();
		show(cat);
		show(dog);
	}

	public static void show(Animal a) {
		a.printInfo();
	}
}

版权声明:本文为lovejojo222原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。