天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
三种初始化字段的方式
1构造函数中赋值
2声明时赋值
3初始化块 initialization block。类中可以包含任意多的代码块,只要构造了类的对象,这些代码块就会被执行。

例子
People.java
 1package test;
 2
 3public class People {
 4    private String name;
 5    private int age;
 6    public int getAge() {
 7        return age;
 8    }

 9    public void setAge(int age) {
10        this.age = age;
11    }

12    public String getName() {
13        return name;
14    }

15    public void setName(String name) {
16        this.name = name;
17    }

18    
19    public People(){
20        //默认构造函数
21    }

22    {
23        this.setAge(12);
24        this.setName("bluesky");
25    }

26
27}

28

Test.java
1package test;
2
3public class Test {
4    public static void main(String args[]){
5        People p = new People();
6        System.out.println(p.getName());
7    }

8
9}
输出bluesky

posted on 2005-11-08 23:11 bluesky 阅读(2201) 评论(0)  编辑  收藏 所属分类: 基础知识


只有注册用户登录后才能发表评论。


网站导航: