【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了TextExtends.java相关资料,希望对各位网友有所帮助!
package com.zf.s5;//创建一个包
class Box{//盒子的父类
double width;//盒子的宽度
double height;//盒子的高度
double depth;//盒子的深度
Box(Box box){//带对象的构造方法
this.width=box.width;
this.height=box.height;
this.depth=box.depth;
}
Box(double width,double height,double depth){//带参数构造方法
this.width=width;
this.height=height;
this.depth=depth;
}
Box(){//默认构造方法
this.width=-1;//用-1表示
this.height=-1;
this.depth=-1;
System.out.println("I am a Box");
}
Box(double len){//构造正方体
this.width=this.height=this.depth=len;
}
double volume(){//求体积
return width*height*depth;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(double w,double h,double d,double m){//带参数的构造方法
width=w;
height=h;
depth=d;
weight=m;
}
BoxWeight(){
System.out.println("I am a small Box");
}
}
public class TextExtends {//操作一个盒子的继承的类
public static void main(String []args){//java程序主入口处
BoxWeight weightBox=new BoxWeight(10,20,15,34.0);//子类实例化对象
Box box=new Box();//超类实例化对象
double vol;
vol=weightBox.volume();//子类调用超类方法
System.out.printf("盒子box1的体积:%s%n",vol);
System.out.printf("盒子box1的重量:%s%n",weightBox.weight);
box=weightBox;
vol=box.volume();//调用方法
System.out.printf("盒子box的体积:%s%n",vol);
//System.out.printf("盒子box的重量:%s%n",box.weight);
Box box2=new BoxWeight();//超类引用子类对象
}
}
上一篇:
TextExample.java
下一篇:
申请开业登记表(个体)