practice8

question8_面向对象三大特性

1、有以下代码,写出该程序运行的结果。

class Super {
	public Super() {
		System.out.println("Super()");
	}

	public Super(String str) {
		System.out.println("Super(String)");
	}
}

class Sub extends Super {
	public Sub() {
		System.out.println("Sub()");
	}

	public Sub(int i) {
		this();
		System.out.println("Sub(int)");
	}

	public Sub(String str) {
		super(str);
		System.out.println("Sub(String)");
	}
}

public class TestSuperSub {
	public static void main(String args[]) {
		Sub s1 = new Sub();
		Sub s2 = new Sub(10);
		Sub s3 = new Sub("hello");
	}
}

Super()、Sub()

Sub()、Sub(int)

Sub(String)

2、看下面代码,写出程序运行的结果

class Super {
	public void m1() {
		System.out.println("m1() in Super");
	}

	public void m2() {
		System.out.println("m2() in Super");
	}
}

class Sub extends Super {
	public void m1() {
		System.out.println("m1() in Sub");
		super.m1();
	}
}

public class TestSuperSub {
	public static void main(String args[]) {
		Sub s = new Sub();
		s.m1();
		s.m2();
	}
}

m1() in Sub

m1() in Super

m2() in Super

3、有如下代码,问:该程序是否能编译通过?如果可以,输出结果是什么?如果不可以,应该如何修改?

class Super {
	public void method() {
		System.out.println("method() in Super");
	}

	public void method(int i) {
		System.out.println("method(int) in Super");
	}
}

class Sub extends Super {
	public void method() {
		System.out.println("method() in Sub");
	}

	public void method(String str) {
		System.out.println("method(String) in Sub");
	}
}

public class TestSuperSub {
	public static void main(String args[]) {
		Super s = new Sub();
		s.method(10);
		s.method();
		s.method("hello");
	}
}

不可以,Super 类中没有定义 method(String)方法,因此无法调用。删除s.method("hello");或在父类中添加带参method(String)方法

4、有如下代码,写出该程序编译运行后输出的结果。

class Super {
	public void m() {
		System.out.println("m() in Super");
	}
}

class Sub extends Super {
	public void m() {
		System.out.println("m() in Sub");
	}
}

public class TestSuperSub {
	public static void foo(Super s) {
		s.m();
	}

	public static void main(String args[]) {
		Sub sub = new Sub();
		Super sup = new Super();
		foo(sup);
		foo(sub);
	}
}

m() in Super

m() in Sub

5、有如下代码,问:下列几个选项中,有哪几个放在//1 位置能够编译通过?

class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}

public class TestAnimal {
	public static void main(String args[]) {
	//主方法代码省略
	}

	public static Animal getAnimal() {
	//1
	}
}

A. return null;

B. return new Animal();

C. return new Dog();

D. return new Cat();

ABCD

6、有如下代码,以上代码有哪些地方编译出错?假设不允许修改MyClass 类,那应该如何修改?

//MyClass.java
package chp8;
public class MyClass{
	private int value;
	public MyClass(){}
	MyClass(int value){
		this.value = value;
	}
	public int getValue(){
		return value;
	}
	public void setValue(int value){
		this.value = value;
	}
}
//TestMyClass1.java
package chp8;
public class TestMyClass1{
	public static void main(String args[]){
		MyClass mc1 = new MyClass();
		MyClass mc2 = new MyClass(10);
		System.out.println(mc1.value);//mc1.getValue
		System.out.println(mc2.value);//mc2.getValue
	}
}
//TestMyClass2.java
package temp;
import chp8.*;
public class TestMyClass2{
	public static void main(String args[]){
		MyClass mc1 = new MyClass();
		MyClass mc2 = new MyClass(10);//此处错误,带参数的构造方法在非同包的类中不能访问
		System.out.println(mc1.value);//mc1.getValue
		System.out.println(mc2.value);//mc2.getValue
	}
}

见注释

7、有如下代码,选择正确答案:

A. 编译通过

B. 编译不通过,应把第12行改成super.value = value;

C. 编译不通过,应把第12行改成super(value);

D. 编译不通过,可以为MySubClass 增加一个value 属性

E. 编译不通过,把第4行改为protected int value; 把第12行改为super.value = value;

//MyClass.java
package chp8;
public class MyClass{
	int value;
}

//MySubClass.java
package temp;
import chp8.MyClass;
public class MySubClass extends MyClass{
	public MySubClass(int value){
		this.value = value;
	}
}

E
子类和父类不在同一个包中,无法访问父类default修饰的属性。
注意:D选项也是正确的,只是会形成属性遮盖。

8、有以下代码,写出这段代码的输出结果。

class Meal {
	public Meal() {
		System.out.println("Meal()");
	}
}

class Lunch extends Meal {
	public Lunch() {
		System.out.println("Lunch()");
	}
}

class Vegetable {
	public Vegetable() {
		System.out.println("Vegetable()");
	}
}

class Potato extends Vegetable {
	public Potato() {
		System.out.println("Potato()");
	}
}

class Tomato extends Vegetable {
	public Tomato() {
		System.out.println("Tomato()");
	}
}

class Meat {
	public Meat() {
		System.out.println("Meat()");
	}
}

class Sandwich extends Lunch {
	Potato p = new Potato();
	Meat m = new Meat();
	Tomato t = new Tomato();

	public Sandwich() {
		System.out.println("Sandwich()");
	}
}

public class TestSandwich {
	public static void main(String args[]) {
		Sandwich s = new Sandwich();
	}
}

Meal()

Lunch()

Vegetable()

Potato()

Meat()

Vegetable()

Tomato()

Sandwich()

9、有以下代码,该程序应该如何修改才能编译通过?

class Super {
}

class Sub extends Super {
	public Sub() {
	}

	public Sub(String str) {
		super(str);
	}

}

为Super增加两个构造函数Super()和Super(String)

10、有如下代码,在//1处,能编译通过的代码为:

A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}

class Super{
	int method(){
	return 0;
	}
}
class Sub extends Super{
// 1
}

AC
A正确,这是方法覆盖的正确形式。
B错误,方法覆盖要求返回值类型相同
C正确,选择C之后Sub类中就有两个method方法,一个是从Super中继承的,另一个是Sub类中定义的。这两个方法方法名相同,参数表不同,构成重载。

11、有如下代码,在//1 处,能编译通过的代码为:

A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
D. private void method(){}

class Super {
	private void method() {}	
}

class Sub extends Super {
	//1
}

ABCD

父类的方法是private方法,无法被子类继承,因此不用考虑重载和覆盖的问题。

12、有如下代码,程序填空:

I. 在 //1, //2, //3 处填上适当的get/set 方法和构造方法
II. 完成//4 处的填空。getAllDog 方法从一个Animal 数组中挑选出所有的Dog对象,并把这些对象放在一个Dog 数组中返回。

class Animal {
	private String name;
	// 1
}

class Dog extends Animal {
	//2
}

class Cat extends Animal {
	//3
}

public class TestAnimal {
	public static void main(String args[]) {
		Animal[] as = new Animal[] { new Dog("Pluto"), new Cat("Tom"), new Dog("Snoopy"), new Cat("Garfield") };
		Dog[] dogs = getAllDog(as);
		for (int i = 0; i <= dogs.length; i++) {
			System.out.println(dogs[i].getName());
		}
	}

	public static Dog[] getAllDog(Animal[] as) {
//4
	}
}

class Animal {
	private String name;
	// 1
	public Animal() {
		super();
	}

	public Animal(String name) {
		super();
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

class Dog extends TestAnimal {

	//2
	private String name;
	public Dog() {
		super();
	}
	
	public Dog(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

class Cat extends TestAnimal {

	//3
	private String name;
	public Cat() {
		super();
	}
	public Cat(String name) {
		super();
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

public class TestAnimal {
	public static void main(String args[]) {
		
		TestAnimal[] as = new TestAnimal[] { new Dog("Pluto"), new Cat("Tom"), new Dog("Snoopy"), new Cat("Garfield") };
		Dog[] dogs = getAllDog(as);
		for (int i = 0; i < dogs.length; i++) {
			System.out.println(dogs[i].getName());
		}
	}

	public static Dog[] getAllDog(TestAnimal[] as) {
		//4
		//计数
		int count = 0;
		for (int i = 0; i < as.length; i++) {
			//判断类型
			if (as[i] instanceof Dog) {
				count++;
			}
		}
		//创建数组
		Dog[] dogs = new Dog[count];
		//下标
		int index = 0;
		for (int i = 0; i < as.length; i++) {
			if (as[i] instanceof Dog) {
				dogs[index] = (Dog)as[i];
				index++;
			}
		}
		
		//遍历
		for (Dog dog : dogs) {
			System.out.println(dog);
		}
		return dogs;
	}
}

13、将Question_07中的Worker、Address类中所有的属性都改成私有,并提供相应的get/set方法

参考12题

14、已知一个类Student 代码如下:

14.1 把Student 的属性都作为私有,并提供get/set 方法以及适当的构造方法。
14.2 为Student 类添加⼀个getPostAddress 方法,要求返回Student 对象的地址和邮编。

class Student {
	String name;
	int age;
	String address;
	String zipCode;
	String mobile;
}

class Student {
	private String name;
	private int age;
	private String address;
	private String zipCode;
	private String mobile;
	
	public Student() {
		super();
	}
	public Student(String name, int age, String address, String zipCode, String mobile) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
		this.zipCode = zipCode;
		this.mobile = mobile;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	
}

15、有以下几个类,根据下面的继承关系,用Java 代码实现。

15.1 Circle 类(圆形),属性:半径;方法:求周长、求面积。
15.2 Rect 类(矩形),属性:长、宽;方法:求周长、求面积。
15.3 Square 类(正方形),属性:边长;方法:求周长、求面积。
提示:
这三个类均具有求周长和面积的方法。
正方形是特殊的矩形。

import java.util.Scanner;

public class Circle {
	private final double PI = 3.14;
	
	public Circle() {
		System.out.println("请输入半径");
		Scanner input = new Scanner(System.in);
		double r = input.nextDouble();
		System.out.println("周长是:"+girth(r));
		System.out.println("面积是:"+area(r));
	}
	public double girth(double r) {
		return 2*PI*r;
	}
	public double area(double r) {
		return PI*r*r;
	}
}

import java.util.Scanner;

public class Rect {
	
	public Rect() {
		Rect();
	}
	public void Rect() {
		System.out.println("请输入矩形的长度:");
		Scanner input = new Scanner(System.in);
		double length = input.nextDouble();
		System.out.println("请输入矩形的宽度:");
		double width = input.nextDouble();
		
		System.out.println("矩形的周长是:"+girth(length, width));
		System.out.println("矩形的面积是:"+area(length, width));
	}
	public double girth(double length,double width) {
		return 2 * length + 2 * width;
	}
	public double area(double length,double width) {
		return length * width;
	}
	
}

import java.util.Scanner;

public class Square extends Rect{
	

	public void Rect() {
		System.out.println("请输入正方形边长:");
		Scanner input = new Scanner(System.in);
		double sideLength = input.nextDouble();
		
		System.out.println("正方形的周长是:");
		System.out.println("正方形的面积是:");
	}
	public double girth(double sideLength) {
		return 4*sideLength;
	}
	public double area(double sideLength) {
		return sideLength*sideLength;
	}
}

import java.util.Scanner;

public class Shape {
	public static void main(String[] args) {
		do {
			System.out.println("======计算图形周长、面积======");
			System.out.println("====[1]圆[2]矩行[3]正方形====");
			Scanner input = new Scanner(System.in);
			System.out.println("请选择:");
			int choice = input.nextInt();
			
			switch (choice) {
			case 0:
				return;
			case 1:
				Circle circle = new Circle();
				break;
			case 2:
				Rect rect = new Rect();
				break;
			case 3:
				Square square = new Square();
				break;
			default:
				System.out.println("请输入正确的命令!");
				break;
			}
		} while (true);
	}
}

16、在上一题的基础上,创建一个长度为3的数组,里面有三个不同类型的对象,分别打印这三个对象的周长和面积。

在上一题的基础上,发现不太好写,于是安安分分的改成封装多态

/**
 *求周长面积,三个类继承它
 */
public class Shape {
	public double girth() {
		return 0;
	}
	public double area() {
		return 0;
	}
}

public class Circle extends Shape{
	private double radius;
	private final double PI = 3.14;
	//get set方法
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double getPI() {
		return PI;
	}
	//带参构造 属性赋值
	public Circle(double radius) {
		super();
		this.radius = radius;
	}
	//重写周长面积方法
	@Override
	public double girth() {
		return 2 * PI * radius;
	}
	@Override
	public double area() {
		return PI * radius * radius;
	}
	
}

public class Rect extends Shape{
	private double length;
	private double width;
	//get set方法
	public double getLength() {
		return length;
	}
	public void setLength(double length) {
		this.length = length;
	}
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	
	/**
	 * 这里要添加一个无参构造,原因是子类继承父类,
	 * 先调用父类的构造方法,父类手动添加过有参构造,
	 * 就不再默认提供无参构造方法了,所以不添加无参构造
	 * 会报错
	 */
	public Rect() {
		
	}
	//带参构造 属性赋值
	public Rect(double length, double width) {
		super();
		this.length = length;
		this.width = width;
	}
	//重写父类周长面积方法
	@Override
	public double girth() {
		return 2 * (length + width);
	}
	@Override
	public double area() {
		return length*width;
	}
	
}

public class Square extends Rect{
	private double length;
	//get set方法
	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
	
	
	public Square(double length) {
		
		this.length = length;
	}

	@Override
	public double girth() {
		return 4 * length;
	}

	@Override
	public double area() {
		return length * length;
	}
	
}

public class TestShape {
	public static void main(String[] args) {
		Shape[] shapes = {new Circle(2),new Rect(2, 3),new Square(2.3)};
		for (int i = 0; i < shapes.length; i++) {
			System.out.println("周长:"+shapes[i].girth()+"面积:"+shapes[i].area());
		}
	}
}

17、某公司的雇员分为以下若干类:

17.1 Employee:这是所有员工总的父类。
17.1.1 属性:员工的姓名,员工的生日月份。
17.1.2 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
17.2 SalariedEmployee:Employee 的子类,拿固定工资的员工。
17.2.1 属性:月薪。
17.3 HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照 1.5 倍工资发放。
17.3.1 属性:每小时的工资、每月工作的小时数。
17.4 SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
17.4.1属性:月销售额、提成率。
17.5 BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
17.5.1 属性:底薪。

要求:
创建SalariedEmployee、HourlyEmployee、SaleEmployee、BasePlusSalesEmployee四个类的对象各一个。
并调用父类getSalary(int money)方法计算某个月这四个对象各自的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。

18、在上一题的基础上,创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。

17、18写一起

/**
 * 所有员工的父类
 */
public class Employee {

	//员工属性 姓名 生日月份
	private String name;
	private int birthMonth;
	
	//构造方法
	public Employee() {
		super();
	}
	public Employee(String name, int birthMonth) {
		super();
		this.name = name;
		this.birthMonth = birthMonth;
	}
	//get set 方法
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getBirthMonth() {
		return birthMonth;
	}

	public void setBirthMonth(int birthMonth) {
		this.birthMonth = birthMonth;
	}
	//父类getSalary方法,子类重写
	public double getSalary(int month) {
		//过生日 奖励100
		if (birthMonth == month) {
			return 100;
		}
		return 0;
	}
}

/**
 *拿固定工资的员工
 */
public class SalariedEmployee extends Employee{
	//基本属性 月薪
	private double salary;

	public SalariedEmployee() {
		super();
	}

	public SalariedEmployee(String name, int birthMonth) {
		super(name, birthMonth);
	}

	public SalariedEmployee(String name, int birthMonth, double salary) {
		super(name, birthMonth);
		this.salary = salary;
	}
	//get set 方法
	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	//重写父类getSalary方法 固定薪资+生日薪资
	@Override
	public double getSalary(int month) {
		return this.salary + super.getSalary(month);
	}
	
}

/**
 * 小时工
 */
public class HourlyEmployee extends Employee{
	//属性 每小时工资 每月工作小时数
	private double salaryPerHour;
	private int hours;
	
	public HourlyEmployee() {
		super();
	}
	
	public HourlyEmployee(String name, int birthMonth) {
		super(name, birthMonth);
	}
	
	public HourlyEmployee(String name, int birthMonth, double salaryPerHour, int hours) {
		super(name, birthMonth);
		this.salaryPerHour = salaryPerHour;
		this.hours = hours;
	}
	//get set 方法
	public double getSalaryPerHour() {
		return salaryPerHour;
	}
	public void setSalaryPerHour(double salaryPerHour) {
		this.salaryPerHour = salaryPerHour;
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	
	//重写父类getSalary方法 小时薪资+超出部分小时薪资+生日薪资
	@Override
	public double getSalary(int month) {
		double sum = 0;
		if (hours > 160) {
			sum = 160 * this.salaryPerHour + (hours - 160) * this.salaryPerHour * 1.5;
		}
		return sum + super.getSalary(month);
	}
	
}

/**
 *销售
 */
public class SalesEmployee extends Employee{
	//属性 月销售额 提成率
	private double monthlySales;
	private double commissionRate;
	//构造方法
	public SalesEmployee() {
		super();
	}
	
	public SalesEmployee(String name, int birthMonth) {
		super(name, birthMonth);
	}
	
	public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
		super(name, birthMonth);
		this.monthlySales = monthlySales;
		this.commissionRate = commissionRate;
	}
	//get set 方法
	public double getMonthlySales() {
		return monthlySales;
	}
	public void setMonthlySales(double monthlySales) {
		this.monthlySales = monthlySales;
	}
	public double getCommissionRate() {
		return commissionRate;
	}
	public void setCommissionRate(double commissionRate) {
		this.commissionRate = commissionRate;
	}

	//重写父类getSalary方法 月销售额*提成率+生日薪资
	@Override
	public double getSalary(int month) {
		return this.monthlySales * this.commissionRate + super.getSalary(month);
	}
	
}

/**
 *有固定底薪的销售人员
 */
public class BasePlusSalesEmployee extends Employee{
	//属性 底薪
	private double basedSalary;
	//构造方法
	public BasePlusSalesEmployee() {
		super();
	}

	public BasePlusSalesEmployee(String name, int birthMonth) {
		super(name, birthMonth);
	}

	public BasePlusSalesEmployee(String name, int birthMonth, double basedSalary) {
		super(name, birthMonth);
		this.basedSalary = basedSalary;
	}

	public BasePlusSalesEmployee(String name, int birthMonth, double basedSalary, double monthlySales, double commissionRate) {
		super(name, birthMonth);
		this.basedSalary = basedSalary;
	}
	////get set 方法
	public double getBasedSalary() {
		return basedSalary;
	}

	public void setBasedSalary(double basedSalary) {
		this.basedSalary = basedSalary;
	}

	//重写父类getSalary方法 基本薪资+过生日
	@Override
	public double getSalary(int month) {
		return this.basedSalary + super.getSalary(month);
	}
		
}

public class TestEmployee {
	public static void main(String[] args) {
		//对各个类创建对象,并调用父类的getSalary方法计算各自工资
		SalariedEmployee se = new SalariedEmployee("john", 5, 5000);
		System.out.println(se.getSalary(7));
		
		HourlyEmployee he = new HourlyEmployee("tom", 10, 25, 170);
		System.out.println(he.getSalary(8));
		
		SalesEmployee sae = new SalesEmployee("Lucy", 7, 200000, 0.03);
		System.out.println(sae.getSalary(8));
		
		BasePlusSalesEmployee bse = new BasePlusSalesEmployee("James", 8, 100000, 0.02, 5000);
		System.out.println(bse.getSalary(8));
		
		System.out.println("=========");
		
		//创建数组
		Employee[] employees = new Employee[] {se,he,sae,bse};
		//遍历打印
		for (int i = 0; i < employees.length; i++) {
			System.out.println(employees[i].getSalary(5));
		}
	}
	
}
posted @ 2021-08-01 11:54  CN_Darren  阅读(372)  评论(0)    收藏  举报