Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1708397
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Java

2008-08-10 22:51:17

1.1. 对象与类

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表示当前实例;

文件:Class.zip
大小:0KB
下载:下载
阅读(1064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~