类的封装性、继承性和多态性设计

(1)请按照以下要求设计一个Student类,并进行测试。(程序1)

要求如下:

1)Student类中包含姓名、成绩两个属性。

 2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值。

3)Student类中定义一个无参的构造方法和一个接受两个参数的构造方法,两个参数分别为姓名和成绩属性赋值。

4)在测试类中创建一个Student对象,一个使用无参构造方法,然后调用方法给姓名和成绩赋值,另一个使用有参的构造方法,在构造方法中给姓名和成绩赋值。

提示:注意成员变量和成员方法的访问权限设置,使用this解决成员变量与局部变量名称冲突的问题

public class Student {
	String name;
	double grade;
	
	public Student(){	
	}
	public Student(String name,double grade){
		this.name = name;
		this.grade = grade;
	}
	public void set(String name,double grade) { 
		this.name = name;
		this.grade = grade;
	}
	public void get() { 
		System.out.println(name);
		System.out.println(grade);
	}
}

public class Test1 {
	public static void main (String args[ ]) {
		Student a1 = new Student();
		a1.set("小明", 100);
		a1.get();
		Student a2 = new Student("小红",99);
		a2.get();
	}
}

(2) 1) 设计一个类,设计它的一个无参数构造方法,构造方法中打印一条消息。main方法创建这个类的一个对象,观察打印结果。

2) 在练习1)的基础上增加一个重载的构造方法,采用一个String参数,并在构造方法中把这个String消息打印出来。创建这个类的对象,观察打印结果。

3) 以练习2)创建的类为基础上,创建属于它的实例对象的一个数组,但不要实际创建对象并分配到数组里。运行程序时,注意是否打印出来自构建方法调用的初始化消息?为数组中每个变量创建对象,观察打印结果?

public class hollow {
	public hollow(){
		System.out.println("This is a massage!");
	}
	public static void main (String args[ ]) {
		hollow a1 = new hollow();
	}
}

public class Test1 {
	String sor;
	public Test1(){
		System.out.println("This is a massage!");
	}
	public Test1(String sor) {
		this.sor = sor;
		System.out.println(sor);
	}
	public static void main (String args[ ]) {
		Test1 a1 = new Test1();
		Test1 a2 = new Test1("This is a Test!");
	}
}

public class Test2 {
	String sor;
	public Test2(){
		System.out.println("This is a massage!");
	}
	public Test2(String sor) {
		this.sor = sor;
		System.out.println(sor);
	}
	public static void main (String args[ ]) {
		Test2 a1 = new Test2();
		Test2 a2 = new Test2("This is a Test!");
		Test2[] a3 = new Test2[1];
		a3[0] = new Test2();
	}
}

(3)用类描述计算机中CPU的速度和硬盘的容量。要求Java应用程序有4个类,名字分别是PC,CPU和HardDisk和Test,其中Test是主类。

public class CPU {
	int speed;
	public void setSpeed(int m){
		this.speed = m;
	}
	public int getSpeed(){
		return speed;
	}
}

public class HardDisk {
	int amount;
	public void setAmount(int m){
		this.amount = m;
	}
	public int getAmount(){
		return amount;
	}
}

public class PC {
	int cpu, HD;
	public void setCPU(CPU c){
		this.cpu = c.speed;
	}
	public void setHardDisk (HardDisk h){
		this.HD = h.amount;
	}
	public void show(){
		System.out.println("cpu的速度为" + cpu);
		System.out.println("硬盘的容量为" + HD);
	}
}

public class Test {
	public static void main (String args[ ]) {
		CPU cpu = new CPU();
		HardDisk disk = new HardDisk();
		PC pc = new PC();
		cpu.setSpeed(2200);
		disk.setAmount(200);
		pc.setCPU(cpu);
		pc.setHardDisk(disk);
		pc.show();
	}
}

设计一个类,类的成员分别用public、private、protected以及“缺省”访问权限。在本类的main方法中创建属于这个类的一个对象,并观察在试图访问所有类的成员时是否会获得编译器错误提示。(为什么?)设计一个新类,并在新类的main方法中创建属于这个类的一个对象,并观察在试图访问所有类的成员时会获得哪种类型的编译器错误提示。(为什么?)

public class old {
	public    String b1 = "public";
	          String b2 = "缺省";
	private   String b3 = "private";
	protected String b4 = "protected";
	public static void main(String arge[]) {
		old n = new old();
		System.out.println(n.b1);
		System.out.println(n.b2);
		System.out.println(n.b3);
		System.out.println(n.b4);
	}
}

public class New {
	public static void main(String arge[]) {
		old n = new old();
		System.out.println(n.b1);
		System.out.println(n.b2);
		System.out.println(n.b3);
		System.out.println(n.b4);
	}
}

写一个类MikeOne,包括一个方法:methodA()打印出类名,放入包mypack.one中,写一个类MikeTwo,包括一个方法:methodB()打印出类名,放入包mypack.two中。定义一个类Cmain,要求在其中可以访问到前面的两个类中的方法。(提示,导入所需包中的类后再访问,获取对象的类名,可以通过对象.getClass().getName()来实现

package mypack.one;

public class MikeOne {
	public void methodA() {
		System.out.println(getClass().getName());
	}
}

package mypack.two;

public class MikeTwo {
	public void methodB() {
		System.out.println(getClass().getName());
	}
}

package mypack.two;
import mypack.one.MikeOne;
public class Camin {
	public static void main(String arge[]) {
		MikeOne a = new MikeOne();
		MikeTwo b = new MikeTwo();
		a.methodA();
		b.methodB();
	}
}

(6)请按照以下要求设计类。( 程序6)

要求如下:

  1. 设计Animal类。类中定义一个成员变量animalName,表示动物的名称,变量animalName是String类型,默认初始值。在该类中再定义一个成员方法shout(),表示动物发出叫声的行为,该方法能输出名称为animalName的动物发出叫声,例如输出“波斯猫发出叫声”。
  2. 设计Cat类。使用关键字extends继承Animal类。Cat类中定义一个public类型的构造方法,该构造方法拥有一个String类型的参数catName,通过该参数,为Cat类中继承自Animal类的animalName成员变量赋值。
  3. 设计一个测试类。类名为ExtendsTest,在main方法中定义一个  String类型的name变量,并为它赋值为”波斯猫”,然后使用有参构造创建Cat类的对象,并使用对象的引用变量调用shout()方法。
public class Animal {
	String animalName;
	void shout() {
		System.out.println(animalName + "发出叫声。");
	}
}

public class Cat extends Animal{
	public Cat(String catName) {
		animalName = catName;
	}
}

public class ExtendsTest {
	public static void main(String arge[]) {
		String name = "波斯猫";
		Cat c = new Cat(name);
		c.shout();
	}
}

 (7) 设计父类和一个子类,在子类里面通过super关键字去调用父类的成员变量和成员方法,在子类的构造方法中使用super关键字调用父类的构造方法,在测试类里进行验证。

public class Father {
	int f1 = 100;
	int f2 = 200;
	public Father(){
		System.out.println(f1);
	}
	void Mother() {
		System.out.println(f1); 
	}
	void FatherAndMother() {
		System.out.println(f2);
	}
}

public class Son extends Father{
	public Son() {
		super();
	}
	void Mother() {
		super.FatherAndMother();
	}
}

public class Test {
	public static void main(String args[]) {
		Son c = new Son();	
		c.Mother();
	}
}

(8)创建平面图形抽象类(PlaneGraphic)及其子类三角形(Triangle), 圆(Circle),长方形(Rectangle)的一个继承分级结构。在抽象类PlaneGraphic中,提供计算图形周长和面积的抽象方法,并在各子类中实现抽象方法,从而根据不同类型的平面图形计算相应的周长和面积。提示:三角形面积计算用海伦公式:,p为三角形的半周长

class Circle extends PlaneGraphic{
	double r;
	public Circle(double r) {
		this.r = r;
	}
	double getCircumference() {
		p = r * 2 * Math.PI;
		System.out.print("该圆形的周长为" + p);
		return p;
	}
	void getArea() {
		area = r *r * Math.PI;
		System.out.println( ",面积为" + area);
	}
}

abstract class PlaneGraphic {
	double area;
	double p;
	abstract void getArea();
	abstract double getCircumference();
}

public class Rectangle extends PlaneGraphic{
	double x, y;
	public Rectangle(double x,double y) {
		this.x = x;
		this.y = y;
	}
	double getCircumference() {
		p = (x + y) * 2;
		System.out.print("该长方形的周长为" + p);
		return p;
	}
	void getArea() {
		area = x * y;
		System.out.println(",面积为" + area);
	}
}

public class Triangle extends PlaneGraphic{
	double a, b, c;
	public Triangle(double a,double b,double c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}
	double getCircumference() {
		p = (a + b + c) / 2;
		return p;
	}
	void getArea() {
		p = getCircumference();
		area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
		System.out.print("该三角形的周长为" + p);
		System.out.println(",面积为" + area);
	}
}

import java.util.Scanner;
public class Test {
	public static void main(String args[]) {
		Scanner num = new Scanner(System. in);
		System.out.println("请输入圆形的半径:");
		double r = num.nextDouble();
		Circle r1 = new Circle(r);
		r1.getCircumference();
		r1.getArea();
		System.out.println("请输入三角形的边长:");
		double a = num.nextDouble();
		double b = num.nextDouble();
		double c = num.nextDouble();
		Triangle abc = new Triangle(a, b, c);
		abc.getCircumference();
		abc.getArea();
		System.out.println("请输入长方形的边长:");
		double x = num.nextDouble();
		double y = num.nextDouble();
		Rectangle xy = new Rectangle(x, y);
		xy.getCircumference();
		xy.getArea();
	}
}

现在要开发一个应用,模拟移动存储设备的读和写功能,即计算机与U盘、MP3、移动硬盘等移动设备进行数据交换。

要求:已知现在要实现计算机对U盘、MP3播放器、移动硬盘这三种移动存储设备的读和写功能,以后可能会有新的第三方的移动存储设备,所以计算机必须有扩展性,能与目前未知而以后可能会出现的存储设备进行数据交换。画出UML图,并实现代码。

abstract class Storage {
	abstract void read();
	abstract void write();
}

public class mobileHardDisk extends Storage{
	void read() {
		System.out.println("移动硬盘正在读取数据!");
	}
	void write() {
		System.out.println("移动硬盘正在写入数据!");
	}
}

public class MP3 extends Storage{
	void read() {
		System.out.println("MP3播放器正在读取数据!");
	}
	void write() {
		System.out.println("MP3播放器正在写入数据!");
	}
}

public class PC {
	public void readFromStorage(Storage storage) {
		storage.read();
	}
	public void writeFromStorage(Storage storage) {
		storage.write();
	}
}

public class USB extends Storage{
	void read() {
		System.out.println("U盘正在读取数据!");
	}
	void write() {
		System.out.println("U盘正在写入数据!");
	}
}

public class Test {
	public static void main(String args[]) {
		mobileHardDisk mhd = new mobileHardDisk();
		USB usb = new USB();
		MP3 mp3 = new MP3();
		PC pc = new PC();
		pc.readFromStorage(mhd);
		pc.writeFromStorage(mhd);
		pc.readFromStorage(usb);
		pc.writeFromStorage(usb);
		pc.readFromStorage(mp3);
		pc.writeFromStorage(mp3);
	}
}

某物流公司可以使用多种交通工具运送货物(汽车,火车,轮船等),未来还有可能更多,请你设计适当的类结构,模拟物流公司运送货物的过程(参见课件上的手机例子),画出UML图,并实现代码。

abstract class Storage {
	abstract void transport();
}

public class TransportTools {
	public void transportFromStorage(Storage storage)
	{
		storage.transport();
	}
}

public class Car extends Storage{
	void transport() {
		System.out.println("使用汽车运输货物!");
	}
}

public class Ship extends Storage{
	void transport() {
		System.out.println("使用轮船运输货物!");
	}
}

public class Train extends Storage{
	void transport() {
		System.out.println("使用火车运输货物!");
	}
}

public class Test {
	public static void main(String args[]){
		Car car = new Car();
		Ship ship = new Ship();
		Train train = new Train();
		TransportTools transportTools = new TransportTools();
		transportTools.transportFromStorage(car);
		transportTools.transportFromStorage(ship);
		transportTools.transportFromStorage(train);
	}
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值