java抽象与封装


打印姓名、性别和年龄

package Server;

public class Person {

     private String name;

     private int age;

     private String sex;

     public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    //自定义构造
    public Person(String sex) {
         this.sex = sex;
     }

    //实例方法或成员方法
  public void introduce(){

     System.out.println("我叫"+name+",芳龄:"+age+",性别:"+sex);


 }





}

输出

package Server;

public class PersonTest {

    public static void main(String[] args) {
          Person per = new Person("男");
//          Person per2 = new Person("男");
          per.setName("小明");
          per.setAge(18);
          per.setSex("女");
          per.introduce();

    }
}

文章作者: 崔蓬勃
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 崔蓬勃 !
评论
  目录