栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java面向对象程序设计[董小园版][习题代码答案]-第七章

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java面向对象程序设计[董小园版][习题代码答案]-第七章

要点回顾:

1、继承:a.被继承的类叫父类(superclass);子类自动继承父类的所有非私有的成员变量和成员方法。

                b.子类与父类是单继承,每个子类只能有一个父类,一个父类可以有好多子类。

2、Object类(所有类的父类):所有类对Object类的继承是Java默认的,无需使用extends明确表示。其中有两个常用方法:

public boolean equals(Object obj);

public String toString();

 需要强调的是:a.对象之间的比较不能用==运算符,必须用equals()方法。

                          b.System.out.print(a);//相当于System.out.print(a.toString());

3、方法覆盖:子类与父类同名,参数表和返回类型完全一样,方法体的内容不同。

注意:a.子类中方法的访问权限不能比父类中被覆盖方法的访问权限低。

           b.子类对父类的非静态方法可以实现方法覆盖。

           c.子类创建的静态方法不会覆盖父类中同名的静态方法。

           d.静态方法不能实现方法重载。

4、关于子类的构造方法:a.系统自动调用

                                          b.子类主动调用(super),且必须放在子类构造方法第一行。

5、this:a.对于static属性和方法,this用不了。

1)使用this可以在类的内部引用该类的其他成员

class Test{
    double width;
    double length;

    void setItem(double width,double length){
        this.width=width;
        this.length=length;
    }
}

2)使用this调用类自己的其他构造方法

class Test{
    String name;
    int age;

    Test(String n){
        name=n;
    }

    Test(String n,int a){
        this(n);
        age=a;
    }
}

 6、super:a.上面子类构造方法中的子类主动调用

                   b.成员变量不能被覆盖,子类和父类若有同名变量,用super.变量名 引用父类变量

                    用super.方法名 引用父类方法

7、父类与子类对象转换

superclass A    subclass B

A  test;

test=new B();//父类对象引向子类实体

test.方法;//调用父类A和子类B共有的方法

注意:a.如果调用的子类方法覆盖了父类的同名方法,用上面的格式会调用子类重写后的方法

           b.可以对test进行子类强制类型转换,使用子类新增的方法 

B test2=(B)test;

test2.子类新增的方法;

实验与训练:

1、注意两个private 成员变量

package train7_1;
import java.util.*;
class Ball{
	private double r;
	
	public void setR(double x) {
		r=x;
	}
	
	public double getR() {
		return r;
	}
}

class Billiards extends Ball{
	Scanner reader=new Scanner(System.in);
	private String color;
	
	public void setCol(String clo) {
		color=clo;
	}
	
	public void show() {
		System.out.print("输入double型半径r:");
		setR(reader.nextDouble());
		System.out.println("该台球的颜色是:"+color+" 半径是:"+getR());
	}
}
public class Train7_1 {

	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		Ball test1=new Ball();
		System.out.print("输入一个double型数值的半径r:");
		test1.setR(reader.nextDouble());
		System.out.println("Ball类赋值得到的半径是:"+test1.getR());
		//这里不能用父类对象指向子类实体,父类和子类各有一个互不相同的私有变量
		Billiards test2=new Billiards();
		System.out.print("输入台球的颜色:");
		test2.setCol(reader.next());
		test2.show();
	}

}

2、

package train7_2;
class Material{
	protected String name;
	protected double price;
	
	Material(String n,double p){
		name=n;
		price=p;
	}
	
	public String toString() {
		System.out.println("name="+name+" price="+price);
		return null;
	}
}

class Wood extends Material{
	private String col;
	
	Wood(String n,double p,String c){
		super(n,p);
		col=c;
	}
	
	 public String toString() {
		System.out.println("name="+name+" price="+price+" col="+col);
		return null;
	}
}
public class Train7_2 {

	public static void main(String[] args) {
		 Material test=new Material("钢筋",2.25);
		 test.toString();
		 test=new Wood("红木",2.369,"red");//父类对象指向子类实体
		 test.toString();
	}

}

思考:(1)产生报错“不能降低自 Material 继承的方法的可视性”

          (2)产生报错“不能降低自 Object 继承的方法的可视性”

原因就是上面方法覆盖注意点a

3、这道题反倒是最简单的,对着例题改就行了哈~

package train7_3;
class Shape{
	String color;
	
	Shape(String c){
		color=c;
	}
	
	double getArea() {
		return 0;
	}
	
	void printInfo() {
		System.out.println("颜色是"+color);
	}
}

class Circle extends Shape{
	double radius;
	
	Circle(String c,double r){
		super(c);
		radius=r;
	}
	
	double getArea() {
		return Math.PI*radius*radius;
	}
	
	void printInfo() {
		super.printInfo();
		System.out.println("面积是"+getArea());
	}
}

class Rectangle extends Shape{
	double width,height;
	
	Rectangle(String c,double w,double h){
		super(c);
		width=w;
		height=h;
	}
	
	double getArea() {
		return width*height;
	}
	
	void printInfo() {
		super.printInfo();
		System.out.println("面积是"+getArea());
	}
}
public class Train7_3 {

	public static void main(String[] args) {
		Shape s;
		System.out.println("父类的引用指向圆形类对象:");
		s=new Circle("blue",1);
		s.printInfo();
		System.out.println("父类的引用指向矩形类对象;");
		s=new Rectangle("green",3,4);
		s.printInfo();
	}

}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号