java 通过构造方法为属性赋值

来源:互联网 发布:中国联通视频软件 编辑:程序博客网 时间:2024/05/18 03:08

源程序:

class Person{
 private String name ;
 private int age ;
 public Person(String name , int age){
  this.setName(name) ;
  this.setAge(age) ;
 }
 public void setName(String name){
  this.name = name ;
 }
 public String getName(){
  return this.name ;
 }
 public void setAge(int age){
  this.age = age ;
 }
 public int getAge(){
  return this.age ;
 }
 public void tell(){
  System.out.println("姓名:" +this.getName() + ",年龄:" + this.getAge()) ;
 }
}
public class Test{
 public static void main(String args[]){
  Person per = null ;
  per = new Person("张三" , 25);
  per.tell() ;
 }
}

运行结果:

java <wbr>通过构造方法为属性赋值

0 0