JAVA中类和对象

一.类的创建:

         比如我们创建狗类:

        

public class Dog {
    public String name; //名字
    public int age;  //年龄
    public double tall;  //身高

    //狗的行为
    public void bark() {
        System.out.println("汪汪汪~~~");
    }

    public void wag() {
        System.out.println("摇尾巴~~~");
    }
}

     注意:

    1.  类名 (Dog) 注意采用大驼峰定义。

     2. 名字,身高这些类的属性要在类的内部方法外部创建。

     3. 一般一个文件当中只定义一个类

     4. 要是想修改类名,不能直接修改在代码 class 后的类名,要在编译器列表的文件栏找到再修改。 

        二.类的实例化:

        前面我们定义了一个类,就相当于在计算机中定义了一种新的类型,与int,double类似,只不过int和double是java语言自 带的内置类型,而类是用户自定义了一个新的类型。

        

public class Dog {
    public String name; //名字
    public int age;  //年龄
    public double tall;  //身高

    //狗的行为
    public void bark() {
        System.out.println("汪汪汪~~~");
    }

    public void wag() {
        System.out.println("摇尾巴~~~");
    }

    public static void main(String[] args) {

        Dog s1 = new Dog();
        Dog s2 = new Dog();

        s1.name = "大黄";
        s1.age = 6;
        s1.tall = 0.4;
        s1.bark();
        s1.wag();

        s2.name = "旺财";
        s2.age = 8;
        s2.tall = 0.45;
        s2.bark();
        s2.wag();

    }
}

       注意:

         1.创建类对象可以采用 类名 + 引用变量名 = new + 类名();  

         2.使用  引用变量名 .  来访问对象中的属性和方法。

        输出结果:

        

        其中前两次动作是大黄的,后两次动作是旺财的。

        做个比方。类实例化出对象就像现实中使用建筑设计图建造出房子,类就像是设计图,只设计出需要什么东西,但是并没有实体的建筑存在,同样类也只是一个设计,实例化出的对象才能实际存储数据,占用物理空间。

        注意:

        

         对于类的成员变量来说,每一个成员变量都有一个默认值,引用类型默认值是 null 基本类型默认为对应的默认值。(int 是 0,double 是 0.0..........)

        三.this的功能

        (1) 

        我们来看下面的代码:

public class Date {

    public int year;
    public int month;
    public int day;

    public void setDate(int y,int m,int d) {
        year = y;
        month = m;
        day = d;
    }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.setDate(1999,6,6);
        date1.printDate();

        Date date2 = new Date();
        date2.setDate(2000,1,10);
        date2.printDate();

    }
}

        打印结果:

             

  我们创建了一个日期(Date)的类,再创建了两个对象,引用变量分别是date1  ,   date2  。

        接着分别赋值后再打印,那么,其他代码不变,我们把 setDate 方法 改成这样

        

public void setDate(int year ,int month,int day) {
        year = year;
        month =month;
        day =  day;
    }

        打印结果:

        

        我们发现对象并没有被赋值,怎么回事呢?

         因为 year = year 与 month = month 与 day = day 都是 setDate 方法里的形参,形参给形参赋值, 并没有赋值给我们的成员变量 date1 和 date2 ,形参名字与成员变量名相同了时,怎么解决呢?

        可以在前面加上 this. 

        

public void setDate(int year ,int month,int day) {
        this.year = year;
        this.month =month;
        this.day =  day;
    }

        打印结果:

        

        可以看到,正常赋值了,因为加上 this 后他会区分是当前对象的变量与形参的 year 与 month 与 day 。

        (2)

        再比如,方法是怎么知道给哪个对象的成员变量进行赋值的呢?

        是因为前面的引用变量名:

    date1.setDate(1999,6,6);
    date1.printDate();

        可以看到,方法前面的  date1. 可以让方法知道你当前调用的是哪个对象。

        而且,方法他会隐藏传递过去一个变量:

        比如我们看一个给对象赋值的方法:

public void setDate(Date this,int year ,int month,int day) {
        this.year = year;
        this.month =month;
        this.day =  day;
    }

        他会隐藏传递一个 Date this , this 可以看作是 date1 引用变量,因为 this 类型也是  Date ,  他把 date1 这个引用隐藏传递过去了,所以为什么只在调用方法前加  date1.  编译器就知道你要修改哪个对象。所以这个 Date this 不需要我们用户写,默认隐藏就行了。

      (3)通过this调用访问当前对象成员方法

        看下面代码:

public class Date {

    public int year;
    public int month;
    public int day;

    public void func() {
        this.printDate();
    }

    public void setDate(Date this,int year ,int month,int day) {
        this.year = year;
        this.month =month;
        this.day =  day;
    }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.setDate(1999,6,6);
        date1.func();
    }
}

        结果:

        

         可以看到,打印我们使用了 date1.func() ; 而  func  方法里面有个 this.printDate() ; 打印我们的结果,因为 this  可以让方法知道你当前调用的是哪个对象,这里我们调用的是 date1 。

        四.就地初始化

看下面代码:

public class Date {

    public int year = 1000;
    public int month = 10;
    public int day = 10;

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.printDate();
    }
}

        结果:

         

         可以看到,我们如果想要默认有参数,就可以这样设置,就不会出现 不初始化 都是 默认基本参数了。

        五.构造方法

                构造方法名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次。

        下面看一段代码:

public class Date {

    public int year;
    public int month;
    public int day;

   public Date(int year ,int month,int day) {
       this.year = year;
       this.month =month;
       this.day =  day;
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date(1999,2,2);
        date1.printDate();
    }
}

        结果:

        

         可以看到,构造方法在新对象时就可以传递过去数据进行初始化,注意,构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。

        而且,当一个类没有写任何构造方法时,此时会存在一个默认不带参数的构造方法,只不过这个默认不带参数的构造方法里面什么都没做

        1.构造方法重载:

        构造方法是可以重载的,比如:

public class Date {

    public int year;
    public int month;
    public int day;

    public Date() {
        System.out.println("什么都没做");
    }

   public Date(int year ,int month,int day) {
       this.year = year;
       this.month =month;
       this.day =  day;
       System.out.println("初始化三个参数");
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date(1999,2,2);
        date1.printDate();
    }
}

        结果:

        

        另一段代码:

public class Date {

    public int year;
    public int month;
    public int day;

    public Date() {
        System.out.println("什么都没做");
    }

   public Date(int year ,int month,int day) {
       this.year = year;
       this.month =month;
       this.day =  day;
       System.out.println("初始化三个参数");
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.printDate();
    }
}

         结果:

        

        可以看到,当  Date date1 = new Date() 括号里有参数 与 没有参数时,结果时不一样的,因为构造方法重载了。

        2.救急不救穷:

        有这段代码:

        

public class Date {

    public int year;
    public int month;
    public int day;

   public Date(int year ,int month,int day) {
       this.year = year;
       this.month =month;
       this.day =  day;
       System.out.println("初始化三个参数");
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.printDate();
    }
}

        结果是报错了,为什么呢?

        因为如果一个构造方法都没有的时候,会有个默认的构造方法,但是其里面什么都不做

        但是上面代码有了个构造方法,就不能调用 默认的构造方法

        俗称“救急不救穷”。

        3.构造方法里可以调用其他构造方法:

public class Date {

    public int year;
    public int month;
    public int day;

    public Date() {
        this(1999,5,5);
        System.out.println("不做什么");
    }

   public Date(int year ,int month,int day) {
       this.year = year;
       this.month =month;
       this.day =  day;
       System.out.println("初始化三个参数");
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.printDate();
    }
}

        结果:

        

         可以看到,他一开始走的是没有参数的 Date 方法,之后通过 this 进入了 有参数 的构造方法,构造方法里可以利用 this 可以调用其他构造方法

        注意:

        this(1999,5,5) ;这段代码必须是构造方法中的第一行!

        4.构造方法不要形成环:

        

public class Date {

    public int year;
    public int month;
    public int day;

    public Date() {
        this(1999,5,5);
        System.out.println("不做什么");
    }

   public Date(int year ,int month,int day) {
        this();
       this.year = year;
       this.month =month;
       this.day =  day;
       System.out.println("初始化三个参数");
   }

    public void printDate() {
        System.out.println(year+" 年 "+month+" 月 "+day+" 日");
    }

    public static void main(String[] args) {

        Date date1 = new Date();
        date1.printDate();
    }
}

        这样也是报错,因为有参数的构造方法里出现了 this () 又回到了无参数的构造方法,形成了环。

        

        

评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值