diff options
| author | crupest <crupest@outlook.com> | 2021-04-27 17:14:23 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2021-04-27 17:14:23 +0800 | 
| commit | 6c0e4b64fa9967b2a2ef7dfb840cab3a8325f67e (patch) | |
| tree | 57176424458aff85325994fc44fd127647100732 /works/life/java-practice/javatest | |
| parent | 0674565dda09fbfba239f56786c0ad4e001279b9 (diff) | |
| download | crupest-6c0e4b64fa9967b2a2ef7dfb840cab3a8325f67e.tar.gz crupest-6c0e4b64fa9967b2a2ef7dfb840cab3a8325f67e.tar.bz2 crupest-6c0e4b64fa9967b2a2ef7dfb840cab3a8325f67e.zip | |
import(life): Add java practice 5.
Diffstat (limited to 'works/life/java-practice/javatest')
5 files changed, 370 insertions, 0 deletions
| diff --git a/works/life/java-practice/javatest/src/javatest/e0501/Main.java b/works/life/java-practice/javatest/src/javatest/e0501/Main.java new file mode 100644 index 0000000..7639bc8 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0501/Main.java @@ -0,0 +1,93 @@ +package javatest.e0501;
 +
 +import java.util.Scanner;
 +
 +interface Soundable {
 +	void makeSound();
 +
 +	void turnUpVolumn();
 +
 +	void turnDownVolumn();
 +}
 +
 +class Radio implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Radio sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +class Walkman implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Walkman sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +class Mobilephone implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Mobilephone sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		Scanner scanner = new Scanner(System.in);
 +		String t = scanner.next();
 +		Soundable soundable = null;
 +
 +		switch (t) {
 +		case "radio":
 +			soundable = new Radio();
 +			break;
 +		case "walkman":
 +			soundable = new Walkman();
 +			break;
 +		case "mobilephone":
 +			soundable = new Mobilephone();
 +			break;
 +		default:
 +			System.out.println("Unknown device.");
 +			System.exit(1);
 +		}
 +		
 +		soundable.makeSound();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0502/Main.java b/works/life/java-practice/javatest/src/javatest/e0502/Main.java new file mode 100644 index 0000000..405a278 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0502/Main.java @@ -0,0 +1,101 @@ +package javatest.e0502;
 +
 +abstract class Animal {
 +	public Animal(String name, String color, String type) {
 +		super();
 +		this.name = name;
 +		this.color = color;
 +		this.type = type;
 +	}
 +
 +	private String name;
 +	private String color;
 +	private String type;
 +
 +	abstract void eat();
 +
 +	abstract void makeSound();
 +
 +	public String getName() {
 +		return name;
 +	}
 +
 +	public void setName(String name) {
 +		this.name = name;
 +	}
 +
 +	public String getColor() {
 +		return color;
 +	}
 +
 +	public void setColor(String color) {
 +		this.color = color;
 +	}
 +
 +	public String getType() {
 +		return type;
 +	}
 +
 +	public void setType(String type) {
 +		this.type = type;
 +	}
 +}
 +
 +class Rabbit extends Animal {
 +	public Rabbit() {
 +		super("兔子", "白色", "哺乳类");
 +	}
 +
 +	@Override
 +	public void eat() {
 +		System.out.println("兔子吃...");
 +	}
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("兔子叫...");
 +	}
 +}
 +
 +interface Swimmable {
 +	void swim();
 +}
 +
 +class Frog extends Animal implements Swimmable {
 +	public Frog() {
 +		super("青蛙", "绿色", "非哺乳类");
 +	}
 +
 +	@Override
 +	public void eat() {
 +		System.out.println("青蛙吃...");
 +	}
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("青蛙叫...");
 +	}
 +
 +	@Override
 +	public void swim() {
 +		System.out.println("青蛙游泳...");
 +	}
 +}
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		Rabbit rabbit = new Rabbit();
 +		Frog frog = new Frog();
 +		
 +		Animal animal = rabbit;
 +		animal.eat();
 +		animal.makeSound();
 +		
 +		animal = frog;
 +		animal.eat();
 +		animal.makeSound();
 +		
 +		Swimmable swimmable = frog;
 +		swimmable.swim();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java b/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java new file mode 100644 index 0000000..b4121af --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java @@ -0,0 +1,60 @@ +package javatest.p0501;
 +
 +abstract class Employee {
 +	public abstract double earnings();
 +}
 +
 +class YearWorker extends Employee {
 +	public double earnings() {
 +		return 50000.456;
 +	}
 +	// 重写earnings()方法
 +}
 +
 +class MonthWorker extends Employee {
 +	public double earnings() {
 +		return 12 * 2300;
 +	}
 +	// 重写earnings()方法。
 +}
 +
 +class WeekWorker extends Employee {
 +	public double earnings() {
 +		return 52 * 500;
 +	}
 +	// 重写earnings()方法。
 +}
 +
 +class Company {
 +	Employee[] employee;
 +	double salaries = 0;
 +
 +	Company(Employee[] employee) {
 +		this.employee = employee;
 +	}
 +
 +	public double salariesPay() {
 +		salaries = 0;
 +		for (int i = 0; i < employee.length; i++) {
 +			salaries = salaries + employee[i].earnings();
 +		}
 +		// 计算salaries。
 +		return salaries;
 +	}
 +}
 +
 +public class HardWork {
 +	public static void main(String args[]) {
 +		Employee[] employee = new Employee[20];
 +		for (int i = 0; i < employee.length; i++) {
 +			if (i % 3 == 0)
 +				employee[i] = new WeekWorker();
 +			else if (i % 3 == 1)
 +				employee[i] = new MonthWorker();
 +			else if (i % 3 == 2)
 +				employee[i] = new YearWorker();
 +		}
 +		Company company = new Company(employee);
 +		System.out.println("公司年工资总额:" + company.salariesPay());
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0502/Road.java b/works/life/java-practice/javatest/src/javatest/p0502/Road.java new file mode 100644 index 0000000..7731494 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0502/Road.java @@ -0,0 +1,70 @@ +package javatest.p0502;
 +
 +interface ComputerWeight {
 +	public double computeWeight();
 +}
 +
 +class Television implements ComputerWeight {
 +	public double computeWeight() {
 +		return 45.5;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class Computer implements ComputerWeight {
 +	public double computeWeight() {
 +		return 65.5;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class WashMachine implements ComputerWeight {
 +	public double computeWeight() {
 +		return 145;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class Car {
 +	ComputerWeight[] goods;
 +	double totalWeights = 0;
 +
 +	Car(ComputerWeight[] goods) {
 +		this.goods = goods;
 +	}
 +
 +	public double getTotalWeights() {
 +		totalWeights = 0;
 +		for (int k = 0; k < goods.length; k++) {
 +			totalWeights = totalWeights + goods[k].computeWeight();
 +		}
 +		// 计算totalWeights
 +		return totalWeights;
 +	}
 +}
 +
 +public class Road {
 +	public static void main(String args[]) {
 +		ComputerWeight[] goodsOne = new ComputerWeight[50], goodsTwo = new ComputerWeight[22];
 +		for (int i = 0; i < goodsOne.length; i++) {
 +			if (i % 3 == 0)
 +				goodsOne[i] = new Television();
 +			else if (i % 3 == 1)
 +				goodsOne[i] = new Computer();
 +			else if (i % 3 == 2)
 +				goodsOne[i] = new WashMachine();
 +		}
 +		for (int i = 0; i < goodsTwo.length; i++) {
 +			if (i % 3 == 0)
 +				goodsTwo[i] = new Television();
 +			else if (i % 3 == 1)
 +				goodsTwo[i] = new Computer();
 +			else if (i % 3 == 2)
 +				goodsTwo[i] = new WashMachine();
 +		}
 +		Car 大货车 = new Car(goodsOne);
 +		System.out.println("大货车装载的货物重量:" + 大货车.getTotalWeights());
 +		Car 小货车 = new Car(goodsTwo);
 +		System.out.println("小货车装载的货物重量:" + 小货车.getTotalWeights());
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java b/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java new file mode 100644 index 0000000..6cead68 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java @@ -0,0 +1,46 @@ +package javatest.p0503;
 +
 +interface drawTest { // 定义接口
 +	public void draw(); // 定义方法
 +}
 +
 +// 定义平行四边形类,该类继承了四边形类,并实现了drawTest接口
 +class ParallelogramgleUseInterface extends QuadrangleUseInterface implements drawTest {
 +	public void draw() { // 由于该类实现了接口,所以需要覆盖draw()方法
 +		System.out.println("平行四边形.draw()");
 +	}
 +
 +	void doAnyThing() { // 覆盖父类方法
 +		// SomeSentence
 +	}
 +}
 +
 +class SquareUseInterface extends QuadrangleUseInterface implements drawTest {
 +	public void draw() {
 +		System.out.println("正方形.draw()");
 +	}
 +
 +	void doAnyThing() {
 +		// SomeSentence
 +	}
 +}
 +
 +class AnyThingUseInterface extends QuadrangleUseInterface {
 +	void doAnyThing() {
 +
 +	}
 +}
 +
 +public class QuadrangleUseInterface { // 定义四边形类
 +	public void doAnyTthing() {
 +		// SomeSentence
 +	}
 +
 +	public static void main(String[] args) {
 +		drawTest[] d = { // 接口也可以进行向上转型操作
 +				new SquareUseInterface(), new ParallelogramgleUseInterface() };
 +		for (int i = 0; i < d.length; i++) {
 +			d[i].draw(); // 调用draw()方法
 +		}
 +	}
 +}
 | 
