赋值
打印值
定义girls类
package com.fqs.demo1; public class Girls { //私有的属性 private String name; //set //get public void setName(String temp) { name=temp; } public String getName() { return name; } private int age; //赋给值 public void setAge(int temp) { if (age<=50 && age>=18) { age=temp; } else { System.out.println("非法输入,请再输入"); } } public int getAge() { return age; } public void Shop() { //购物 } }
grilsTest类 赋值 打印值
package com.fqs.demo1; public class GirlsTest { public static void main(String[] args) { Girls myG=new Girls(); //赋给值 myG.setAge(80); //打印获取的值 System.out.println(myG.getAge()); } }
标签:封装,String,temp,age,Girls,public,void From: https://www.cnblogs.com/haha1988/p/17142472.html