Java基础(极客)——11、Java面向对象中引用的传递

package com.zhh.java.mianxiangduixiang3;


/**
 * 
 * 1、java引用传递
 */

public class YingYongChuanDi1 {


    public static void main(String[] args) {
        Ref1 r1 = new Ref1();
        r1.temp = 20;
        System.out.println(r1.temp);
        mathod1(r1);
        System.out.println(r1.temp);


    }


    public static void mathod1(Ref1 r2) {
        r2.temp = 30;


    }


}


class Ref1 {
    int temp = 10;


}


package com.zhh.java.mianxiangduixiang3;


/**
 * 
 * 1、java引用传递
 */

public class YingYongChuanDi2 {
    public static void main(String[] args) {
        String str1 = "hello";
        System.out.println(str1);
        tell(str1);
        System.out.println(str1);
        /*打印出来的两次str1的值都是hello因为String的字符串是不能改变的,其他数据类型就变了
         * 
         */


    }


    public static void tell(String str2) {
        str2 = "jake";
    }
}



package com.zhh.java.mianxiangduixiang3;


/**
 * 
 * 1、java引用传递
 */

public class YingYongChuanDi3 {
    public static void main(String[] args) {


        MyRef1 r1 = new MyRef1();
        r1.temp = "jike";
        System.out.println(r1.temp);
        tell(r1);
        System.out.println(r1.temp);
    }


    public static void tell(MyRef1 r2) {
        r2.temp = "xueyuan";
    }
}


class MyRef1 {
    String temp = "hello";
}



package com.zhh.java.mianxiangduixiang3;


/**
 * 2、java this关键字
 * 1)表示类中的属性和调用方法
 * 2)调用本类中的构造方法
 * 3)表示当前对象
 * 
 */

public class MyThisDemo1 {
    public static void main(String[] args) {
        new Person("zhang3", 30).toString();
    }
}


class Person {
    private String name;
    private int    age;


    public Person() {
        System.out.println("无参构造方法被调用了");
    }


    Person(String name, int age) {
        this();//调用无参构造方法,必须放在第一行
        this.name = name;//this.name表示本类中的属性name
        this.age = age;//this.age表示本类中的属性age
    }


    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;
    }


    @Override
    public String toString() {
        //this.getName()表示当前调用的是本类中的getName()
        //this.getAge()表示当前调用的是本类中的getAge()
        //现在本类中getName() getAge()只有一个,不写this默认就是调用本类的
        System.out.println(this.getName() + ":" + this.getAge());
        return null;
    }


}


package com.zhh.java.mianxiangduixiang3;


/**
 * 2、java this关键字
 * 1)表示类中的属性和调用方法
 * 2)调用本类中的构造方法
 * 3)表示当前对象
 * 
 */

public class MyThisDemo2 {
    public static void main(String[] args) {
        Person2 person = new Person2();
        System.out.println(person);
        person.tell();
        /*打印结果是同一对象
         *com.zhh.java.mianxiangduixiang3.Person2@175d6ab
          com.zhh.java.mianxiangduixiang3.Person2@175d6ab
         * 
         */


    }


}


class Person2 {
    public void tell() {
        System.out.println(this);//this表示当前对象
    }


}



package com.zhh.java.mianxiangduixiang3;


/**
 * 3、java static关键字
 *static声明的属性是类变量(全局属性)
 *static声明的方法是类方法
 *属性和方法调用的时候直接通过类调用
 *使用static方法的时候,只能调用static的属性和方法
 * 
 * 
 */

public class MyStaticDemo1 {
    public static void main(String[] args) {
        Person3 person1 = new Person3("张3");
        Person3.county = "上海";//因为county被static修饰所以修改一次,全局都修改了,其他对象掉用这个属性值就是上海
        person1.tell();
        Person3 person2 = new Person3("张4");
        person2.tell();
        Person3 person3 = new Person3("张5");
        person3.tell();
    }
}


class Person3 {
    private String name;
    static String  county = "北京";


    public Person3(String name) {
        this.name = name;
    }


    public void tell() {
        System.out.println(name);
        System.out.println(county);
    }
}



package com.zhh.java.mianxiangduixiang3;


/**
 * 3、java static关键字
 *static声明的属性是类变量(全局属性)
 *static声明的方法是类方法
 *属性和方法调用的时候直接通过类调用
 *使用static方法的时候,只能调用static的属性和方法
 * 
 * 
 */

public class MyStaticDemo2 {
    public static void main(String[] args) {
        Person4 person1 = new Person4("张3");
        Person4.setCounty("上海");//调用类方法,静态的都在对象实例化之前已经执行了
        person1.tell();
        Person4 person2 = new Person4("张4");
        person2.tell();
        Person4 person3 = new Person4("张5");
        person3.tell();
    }
}


class Person4 {
    private String        name;
    private static String county = "北京";


    public Person4(String name) {
        this.name = name;
    }


    public String getName() {
        return name;
    }


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


    public static String getCounty() {
        return county;
    }


    public static void setCounty(String county) {
        Person4.county = county;
    }


    public void tell() {
        System.out.println(name);
        System.out.println(county);
    }
}



package com.zhh.java.mianxiangduixiang3;


/**
 * 3、java static关键字
 *static声明的属性是类变量(全局属性)
 *static声明的方法是类方法
 *属性和方法调用的时候直接通过类调用
 *使用static方法的时候,只能调用static的属性和方法
 * 
 * 
 */

public class MyStaticDemo3 {
    private static int i;


    public static void main(String[] args) {
        //        使用static方法的时候,只能调用static的属性和方法
        System.out.println(i);
        method();


    }


    public static void method() {
        System.out.println("静态方法被调用了");


    }
}




源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值