栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

超详解java的类与对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

超详解java的类与对象

什么是面向过程?

每一步都要自己去参与,如果我们敲代码也像洗衣服一样,将来扩展或者维护起来会比较麻烦

什么是面向对象?

如果我们用洗衣机,那么我们不用关心洗衣服这个过程,只要把衣服交给洗衣机即可。这就是面向对象,对象有人,衣服,洗衣机,洗衣粉。

用类来描述对象

在java中我们用class关键字来定义类。

class ClassName{ //采用大驼峰
field; // 字段(属性) 或者 成员变量
method; // 行为 或者 成员方法 
}

例如我们定义一个小狗

定义一个类
public class Dog {
    //成员变量
    public String name;
    public int age;
    public String color;
    //方法
    public void wag(){
        System.out.println(name+"摇尾巴");
    }
    public void bark(){
        System.out.println(name+"旺旺旺");
    }
    public void show(){
        System.out.println("名字:"+name+" 年龄:"+age+"颜色:"+color);
    }
}
new 关键字用于创建一个对象的实例 . 使用 . 来访问对象中的属性和方法 . 同一个类可以创建对个实例
public class Test {
    public static void main(String[] args) {
        Dog dog1=new Dog();
        dog1.name="小黑";
        dog1.age=3;
        dog1.color="黑色";
        dog1.bark();
        dog1.wag();
        dog1.show();

        Dog dog2=new Dog();
        dog2.name="饱饱";
        dog2.age=1;
        dog2.color="白色";
        dog2.bark();
        dog2.wag();
        dog2.show();
        //可以通过一个类实例化多个对象
    }
}

注意上面代码是放在2个文件中,因为最好一个文件放一个类。

this的用法
public class Date {
    public int year;
    public int month;
    public int day;
    //类中的方法不加static
    public  void setDate(int year,int month,int day){
        year=year;
        month=month;
        day=day;
    }
    public  void printDate(){
        System.out.println(year+"/"+month+"/"+day);
    }
}
 public static void main(String[] args) {
        Date date1=new Date();
        Date date2=new Date();
        Date date3=new Date();
        date1.setDate(2022,5,9);
        date1.printDate();
    }

为什么打印出来的不是2022/5/9,而是0/0/0? 在setDate函数中成员变量和形参名相同。

那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计自己都搞不清楚了。所以引入this。
public class Date {
    public int year;
    public int month;
    public int day;
    //类中的方法不加static
    public  void setDate(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public  void printDate(){
        System.out.println(this.year+"/"+this.month+"/"+this.day);
    }
}

这时候打印出来的就是2022/5/9

this 引用指向当前对象 ( 成员方法运行时调用该成员方法的对象 ) ,在成员方法中所有成员变量的操作,都是通过该 引用去访问. this引用的特性 1. this 的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型 2. this 只能在 " 成员方法 " 中使用 3. 在 " 成员方法 " 中, this 只能引用当前对象,不能再引用其他对象 对象的构造和初始化

我们可以发现在上面代码中我们要通过setDate来设置日期,非常麻烦,我们需要更简单的初始化。

1.构造方法 构造方法 ( 也称为构造器 ) 是一个特殊的成员方法, 名字必须与类名相同,在创建对象时,由编译器自动调用,并且 在整个对象的生命周期内只调用一次
public class Date {
    public int year;
    public int month;
    public int day;
    //类中的方法不加static
    public Date(){//名字与类名相同,没有返回值类型,设置为void也不行。 一般情况下使用public修饰
        this.year=0;
        this.month=0;
        this.day=0;
    }
    public  void setDate(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public  void printDate(){
        System.out.println(this.year+"/"+this.month+"/"+this.day);
    }
}

经过构造方法,在不调用setDate的情况下,可以打印出初始化的日期0/0/0。

public class Date {
    public int year;
    public int month;
    public int day;
    //类中的方法不加static
    public Date(){//名字与类名相同,没有返回值类型,设置为void也不行。 一般情况下使用public修饰
        this.year=0;
        this.month=0;
        this.day=0;
    }//这是不带参数的构造方法
    public Date(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
        System.out.println("这是带3个参数的构造方法");
    }
    public  void setDate(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public  void printDate(){
        System.out.println(this.year+"/"+this.month+"/"+this.day);
    }
}
public static void main(String[] args) {
        Date date1=new Date(2002,11,19);
        //date1.setDate(2022,5,9);
        date1.printDate();
    }

构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)所以我上面既有无参构造方法,也有3个参数构造方法。

如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的

这也是为什么我们在类不初始化成员变量也可以通过。

构造方法中,可以通过this调用其他构造方法来简化代码

public class Date {
    public int year;
    public int month;
    public int day;
    //类中的方法不加static
        public Date(){
            this(1999,9,19);
        }
//    public Date(){//名字与类名相同,没有返回值类型,设置为void也不行。 一般情况下使用public修饰
//        this.year=0;
//        this.month=0;
//        this.day=0;
//    }//这是不带参数的构造方法
    public Date(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
        System.out.println("这是带3个参数的构造方法");
    }
    public  void setDate(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public  void printDate(){
        System.out.println(this.year+"/"+this.month+"/"+this.day);
    }
}
 public static void main(String[] args) {
        Date date1=new Date();
        //date1.setDate(2022,5,9);
        date1.printDate();
    }

通过this调用3个参数的构造方法,初始化为1999/9/19

注意事项:this(...)必须是构造方法中第一条语句

不能形成环,例如
public Date(){ 
this(1900,1,1); 
}
public Date(int year, int month, int day) {
 this(); 
}
无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号