java 封装性

“equals”和“==”不一样,前者比较内容是否一致,后者比较对象是否一样(即使两个对象名称完全一样,也不符合==的逻辑)

******************************************

构造方法作业:

class Person
{
    private int age=-1;
    private String name="unknown";
    void shout()
    {
        //int age=60;
        System.out.println(name+"my age is"+age);
    }
    /*
    public Person () {
        age= 10;
        System.out.println("the constructor1 is calling!");
    }
    */
    public Person(String x)
    {
        name = x;
    }
    public Person(String x,int y)
    {
        name =x;
        age = y;
    }
    
    public void setAge(int x)
    {
        if(age<0)
            return;
        age = x;
    }
    public int getAge()
    {
        return age;
    }
    public static void getSomeOne(Person p)
    {
        p.shout();
    }
    public static void main(String [] args)
    {
        Person p1 = new Person();
        Person p2 = new Person("zhangsan,20");
        Person p3 = new Person("wangwu");
        p1.age= -30;
        p1.shout();
        p2.shout();
        p3.shout();
        
        //p1.getSomeOne(p2);
        
        /*String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = str1;
        if(str1.equals(str2))
            System.out.println("true");
        else {
            System.out.println("false");
        }*/
        //getSomeOne(new Person());
    }
}
class TextPerson
{
    public static void main(String [] args)
    {
        /*Person p1 = new Person();
        Person p2 = new Person();
        p1.age= -30;
        p1.shout();
        p2.shout();
        */
        
        /*String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = str1;
        if(str1.equals(str2))
            System.out.println("true");
        else {
            System.out.println("false");
        }*/
        //getSomeOne(new Person());
    }
    public static void getSomeOne(Person p)
    {
        p.shout();
    }

}


***************************************************

this的用法

<span style="font-size:14px;">    public Person(String name)
    {
        this.name = name;
    }
    public Person(String name,int age)
    {
        this.name = name;
        this.age = age;
    }</span>




********************************************************

垃圾回收:finalize方法


import java.awt.Component;

class Person
{
    private int age=-1;
    private String name="unknown";
    void shout()
    {
        //int age=60;
        System.out.println(name+"my age is"+age);
    }
    
    public Person () {
        age= 10;
        System.out.println("the constructor1 is calling!");
    }
    
    public Person(String name)
    {
        this.name = name;
    }
    public Person(<span style="color:#FF0000;">String name,int age</span>)
    {
        /*this.name = name;*/
        <span style="color:#FF0000;">this(name);</span>
        <span style="color:#FF0000;">this.age = age;</span>
    }
    public void <span style="color:#FF0000;">finalize</span>()
    {
        System.out.println("object is going");
    }
    
    public void setAge(int x)
    {
        if(age < 0)
            return;
        age = x;
    }
    public int getAge()
    {
        return age;
    }
    public static void getSomeOne(Person p)
    {
        p.shout();
    }
    public void fun1()
    {
        System.out.println(name);
    }
    public void fun2()
    {
        Person a2 = new Person("a2");
        a2.fun1();
        this.fun1();
    }
    public static void main(String [] args)
    {
        /*Person a1 = new Person("a1");
        a1.fun2();
        */
        
        new Person();
        new Person();
        new Person();
        System.gc();
        
        /*Person p1 = new Person("ni");
        Person p2 = new Person("zhangsan,20");
        Person p3 = new Person("wangwu");
        p1.age= -30;
        p1.shout();
        p2.shout();
        p3.shout();
        */
        
        //p1.getSomeOne(p2);
        
        /*String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = str1;
        if(str1.equals(str2))
            System.out.println("true");
        else {
            System.out.println("false");
        }*/
        //getSomeOne(new Person());
    }
}
class TextPerson
{
    public static void main(String [] args)
    {
        /*Person p1 = new Person();
        Person p2 = new Person();
        p1.age= -30;
        p1.shout();
        p2.shout();
        */
        
        /*String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = str1;
        if(str1.equals(str2))
            System.out.println("true");
        else {
            System.out.println("false");
        }*/
        //getSomeOne(new Person());
    }
    public static void getSomeOne(Person p)
    {
        p.shout();
    }

}
/*
class Container
{
    Component comp;
    public void addCompenent(Component comp)
    {
        comp = new Component(new Container());
    }
}
*/

/*
class Component
{
    Container c;
    public Component(Container c)
    {
        this.c=this.c;
    }
}

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值