java属性

来源:百度知道 编辑:UC知道 时间:2024/05/18 02:55:01
举个例子说明一下,什么是属性与什么是方法?还有构造方法!

class Student {
String name; // Student对象的属性名字
int age;// Student对象 的属性学生年龄
Student (String name,int age) {
this.name=name;
this.age=age;
} //构造方法,为实例变量(属性)赋初值
void sleep() {
System.out.println( "have a rest");
} //Student对象的方法睡觉

}

pubic class Test {
public static void main(String[] args) {
Student s = new Student("Tom",16); //通过new调用Student的构造方法创建了一个Student对象
s.sleep(); //调用Student对象的睡觉方法
}
}

public class Images {
private int x=0;//这是属性
private int y=0;//这是属性
private int width=0;//这是属性
private int height=0;//这是属性

public Images(){

}//这是构造方法。与类同名,没有返回值。

public String getNewImage(){

}//这 是方法。String返回类型。getNewImage是方法名称。()可带参数

public class Text{
private String name="";//成员变量又叫属性
public Text(){}//构造方法
public void getInfo(){//普通方法