要定义一个类Student,有属性Name和Age,
在C#中可以这样写
public class Student
{
private string _Name;
private int _Age;public string Name
{
get{return this._Name;}
set{this._Name=value;}
}public int Age
{
get{return this._Age;}
set{this._Age=value;}
}使用时可以:
Student stu = new Student();
stu.Name="张三";
stu.Age=12;
}最近要用到java,但是不知道怎样定义这样的类,我找了很久都没找到例子。
能不能给一个简单的示例?

解决方案 »

  1.   

    package com.iwtxokhtd.news.bean;public class Student { private String name;
    private int age;
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public static void main(String []args){
    Student stu=new Student();
    stu.setAge(20);
    stu.setName("abc");
    }

    }
      

  2.   


    public class Student{
      private String name;
      private int age;
      public void setName(String name){
         this.name = name;
      }
      public void setAge(int age){
         this.age = age;
      }
      public String getName(){
         return this.name;
      }
      public int getAge(){
         return this.age;
      }
    }//使用的时候
    Student student = new Student();
    student.setName("张三");
    student.setAge(12);