package jvxing; public class Jvxing { //成员变量 private double width; private double chang; public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getChang() { return chang; } public void setChang(double height) { this.chang = height; } //成员方法 public double mianji() { return this.width*this.chang; } }
package jvxing; public class Cft extends Jvxing { //定义新的成员变量高 private double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } //定义新的成员方法求体积 public double tiji() { return this.getChang()*this.getHeight()*this.getWidth(); } }
package jvxing; public class Text_jvxing { public static void main(String[] args) { //实例化矩形对象求面积 Jvxing j= new Jvxing(); j.setWidth(10); j.setChang(15.3); System.out.println("矩形的长为:"+j.getChang()+" 宽为:"+j.getWidth()); System.out.println("矩形的面积为:"+j.mianji()); //实例化长方体对象求体积 Cft c = new Cft(); c.setChang(2); c.setHeight(3.2); c.setWidth(100); System.out.println("长方体的长为:"+c.getChang()+" 宽为:"+c.getWidth()+" 高为:"+c.getHeight()); System.out.println("长方体的体积为:"+c.tiji()); } }
时间: 2024-10-26 03:03:50