类 封装
package com.fqs.demo061201; public class Girl { //属性 String name; private int age; //set赋值 public void setAge(int a){ if (age>18&& age<50){ age=a; }else{ System.out.println("年龄不合法"); } } //get获取 public int getAge(){ return age; } //方法 public void shop() { System.out.println("购物"); } public void play() { System.out.println("玩游戏"); } }
调用方法
package com.fqs.demo061201; public class GirlTest { public static void main(String[] args) { Girl g=new Girl(); Girl g2=new Girl(); g.name="doudou"; g.setAge(180); System.out.println(g.name+" "+g.getAge()); g.play(); g.shop(); //第二个女朋友 g2.name="dou2"; g2.setAge(-19); System.out.println(g2.name+" "+g2.getAge()); } }
标签:传参,set,g2,setAge,83,Girl,public,name From: https://www.cnblogs.com/haha1988/p/17476103.html