public class Lader {
//定义类
float above; //成员变量,类中有效
float bottom;
float height;
float area;
// float area=4 //合法
// area=4 //非法,在方法体中赋值
float coputerArea(){
//定义方法
area=(above+bottom)*height/2.0f;
return area;
}
void setHeight(float h){
//定义方法
height=h;
}
class Workman{
double x; //声明局部变量
//构造方法,名和所在的类相同,无类型
Workman(){
//无参构造方法
}
Workman(int a,int b){
//有参构造
}
int Workman(){
//不是构造方法,为普通方法
return 12;
}
}
void f(){
float height=4;
int m; //局部变量没有默认值
area=(above+bottom)*this.height/2.0f;
}
}
class Studen {
public static void main(String[] args) {
Lader lad; //声明对象
lad = new Lader(); //为对象分配变量(使用new和默认构造方法)【lad对象包含Lader类中的above,height,area等变量】
System.out.print(lad.area); //使用对象调用成员变量
float are=lad.coputerArea(); //使用对象调用类方法
System.gc();
}
}
Java类、构造方法、对象
猜你喜欢
转载自blog.csdn.net/weixin_45142629/article/details/118187888
今日推荐
周排行