今天开始学习面向对象:
首先学习了一下构造方法,和普通的成员方法。
public class TestBoxVloumnParent {
int length;
int height;
int width;
}这是父类
//构造方法,构造器,作用
//用来藐视对象的创建过程,就是用来创建对象,给属性赋初始值,
//或者做其他初始化操作
//月饼
// 制作(构造方法)
//月饼的模板--------------------月饼
// 参数
//java中的构造方法,在类当中声明,方法名跟类名一直,而且构造方法没有返回值
//当创建对象的时候,就立刻调用构造方法,来给对象初始化,
//对象的实例化过程
/*
* this 关键字,this,当前指代当前对象,(对当前对象的引用)
* 企业开发当中,构造方法的参数的名字跟属性名保持一致
* 需要时用this来区分,this.属性名//当前这个对象的属性
* this.方法名//当前这个对象的方法名
* 利用构造方法创建一个正方体对象,
* new的时候回去调用只有一个参数的构造方法来生成对象并初始化
* */
public class TestBoxVloumn1 extends TestBoxVloumnParent {
/*int length;
int height;
int width;*/
TestBoxVloumn1(int length,int height,int width){
super.length=length;
super.height=height;
super.width=width;
}
/**
* @param i
* @param j
*/
// public TestBoxVloumn1(int height, int length) {
// super.height=height;
// super.length=length;
// }
public static void main(String[] args) {
TestBoxVloumn1 TBV=new TestBoxVloumn1(2,3,5);
//TestBoxVloumn1 TBV2=new TestBoxVloumn1(2,3);
System.out.println(TBV.Vloumn());
System.out.println(TBV.Vloumn(1));
}
int Vloumn(){
return length*height*width;
}
int Vloumn(int i){
return length*height*width+1;
}
}
1万+

被折叠的 条评论
为什么被折叠?



