Java 实验 实验五-1

5_1上机实践

1.继承:在同一包中,子类继承所有父类非私有的成员变量、成员方法。

  1. 定义圆类,成员变量半径r;

 方法:半径的set、get方法;求面积Area、周长Length方法。

2)定义圆柱类继承圆类,定义成员变量:高 h,

方法:高的set、get方法;求体积方法(调用圆面积方法*h)

3)定义测试类:用圆柱体类创建对象,求体积。

class Circle {     

    double radius;

    void setRadius(double r) {

        radius=r;

    }

    double getRadius() {

      return radius;

   }

double Length(){

        return 2*Math.PI*radius;

}  //求周长方法Length返回周长

double Area(){

    return Math.PI*radius*radius;

}   //求面积方法Area返回面积

}

class Cylinder extends Circle// 继承圆类

 {

    double height;

    void setHeight(double h) {

       height = h;

}

     double getHeight() {

       return  height;

}

void Show() {

     System.out.println("半径:"+radius);

}

  double Volume() {

       return   Area()*height; //调用Area方法求体积

}

}

public class test {

   public static void main(String args[]) {

       Cylinder cy = new Cylinder();  //创建圆柱体对象

        cy.setRadius(1);//cy 调用set方法给半径赋值为1

cy.setHeight(3);//cy 调用set方法给高赋值 3

cy.Show();

System.out.println("半径:"+cy.getRadius());

       System.out.println("圆底面积:"+cy.Area());

       System.out.println("圆的体积:"+ cy.Volume());  

 }

}

  1. 继承:不在同一包中,子类继承父类public和protected的成员变量、成员方法。

修改程序1,将Circle类移到另一包中。

总结:protected 成员只能被不同包的( 子类中  )所访问。

  1. 修改程序1,定义Circular类继承Cylinder类

   在主方法中测试 instanceof。

  System.out.println(cy  instanceof  Cylinder); //Object、Circle、Circular、String

  1. 成员变量的隐藏

 修改程序1,在Cylinder 类中定义半径 radius=10,

      void Show() {

    System.out.println("圆柱半径:"+radius+"   圆半径:"+super.radius);

}   

运行结果:

圆柱半径:10.0   圆半径:5.0

结论:子类定义了和父类同名的成员变量,则在子类中父类的同名变量可通过(super.)访问。

  1. 方法的重写,注,重写方法时不能降低访问控制权限。

  修改程序4,在Cylinder 类中添加Area方法:

 double Area(double r) {   //该方法与Circle类中的Area方法的关系是重载

          return Math.PI*r*r;

   }

 double Area() { //求圆柱体表面积,该方法与Circle类中的Area方法的关系是重写

          return super.Area()*2+Length()*height;

   }

程序的运行结果:

运行结果:

圆柱半径:10.0   圆半径:5.0

半径:5.0

圆的面积:201.06192982974676

圆柱的体积:235.61944901923448

运行结果:

圆柱半径:10.0   圆半径:1.0

半径:1.0

圆柱的表面积:25.132741228718345

圆柱的体积:9.42477796076938

  1. 子类构造方法调用父类构造方法

在下面程序的Circle和Cylinder类中添加默认的构造方法。

class Circle {

 double radius;

Circle() { }// 默认构造方法

 void setRadius(double r) {

radius = r;

}

double getRadius() {

return radius ;

}

double length() {

return  2*Math.PI* radius ;

}

void Show() {

     System.out.println("半径:"+radius);

}

double Area() {

return Math.PI* radius * radius;

}

}

class Cylinder extends Circle {

double height;

Cylinder() { super(); }// 默认构造方法

void setHeight(double h) {

height = h;

}

double getVolme() {

return super.Area() * height;

}

double Area() {

return super.Area() * 2 + length() * height;

}

}

public class test {

public static void main(String args[]) {

Cylinder cy = new Cylinder();

System.out.println(cy.radius);

System.out.println("圆柱的bottom面积:" + cy.Area());

System.out.println("圆柱的体积:" + cy.getVolme());

}

}

运行结果:

0.0

圆柱的bottom面积:0.0

圆柱的体积:0.0

运行结果:

圆柱

圆柱半径:1.0  圆半径:1.0

半径:Cylinder{height=3.0,radius=1.0}

圆柱的面积:25.132741228718345

圆柱的体积:9.42477796076938

  1. 修改上题,分别给Circle和Cylinder类中添加有参构造方法给成员变量赋值。

运行结果:2.0

圆柱的bottom面积:62.83185307179586

圆柱的体积:37.69911184307752

总结:

1.final    类是最终类,不能被继承

2.final   方法是最终方法,不能被子类重写

3.final   变量是常量,只能赋一次值

  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值