java 类的封装范例1

2019年2月15日21:56:36 发表评论 1,003 views
/**
 * Created by xabcd on 2019/2/15.
 */
public class Person
{
    String name;
    int age;
    void talk()
    {
        System.out.println("我是:"+name+",今年:"+age+"岁");
    }
}





/**
 * Created by xabcd on 2019/2/15.
 */
public class testPersonDemo2
{
    public static void main(String args[]) {
        //声明并实例化一个对象
        Person p = new Person();
        //给p中的属性赋值
        p.name = "张三";
        p.age = -25;
        //调用Person类中的talk()方法
        p.talk();
    }

}







结果:
我是:张三,今年:-25岁


发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: