分类: Java
2008-08-10 22:51:17
1.1.1. 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
class Shape{
int x;
int y;
Shape(){
x = 10;
y = 20;
System.out.println("Shape");
}
Shape(int x,int y){
this.x = x;
this.y = y;
System.out.println("Shape");
}
public void setX(int x){
this.x = x;
}
public int getX(){
return x;
}
public void setY(int y){
this.y = y;
}
public int getY(){
return this.y;
}
}
public class Class {
public static void main(String [] args){
Shape sp = new Shape();
sp.setX(100);
sp.setY(200);
System.out.println("x = " + sp.getX() + ";y = " + sp.getY());
}
}
1.1.2. 说明
1. 创建一个类:class className{}
2. 定义一个类的成员变量x,y;
3. 创建不带参数类方法;getX,getY
4. 创建带参数类方法:setY,setY
5. 重载函数Shape();Shape(int,int)
6. 类成员、方法的可见性public/package/privte
7. 创建类对象(实例化)Shape sp = new Shape();或Shape sp = new Shape(10,10);
8. 访问类成员或方法;instance.field;instance.method
9. this表示当前实例;
|