类和对象是什么?(下)

简介: 类和对象是什么?

类和对象是什么?(上):https://developer.aliyun.com/article/1517679

3.2.this的使用

this表示当前对象的引用,有对象。

我们先来看一个现象。

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
    public Dog(String name,int age,char sex){
        name = name;
        age = age;
        sex = sex;
    }
 
 
 
    void print(){
        System.out.println("姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex);
    }
 
}
 
public class test {
    public static void main(String[] args) {
        Dog dog = new Dog("张三",19,'男');
        dog.print();
 
    }
}

 

使用this后

2.this调用类中方法

若不同参数的构造方法之间出现了重复调用,可以使用this(参数)调用其他的构造方法

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
    public Dog(String name,int age,char sex){
        this(age);//要放在第一句
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Dog(int a){
        System.out.println("一个参数的有参构造");
    }
 
    public Dog(){
        System.out.println("我是无参构造");
    }
 
    void print(){
        System.out.println("姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex);
    }
 
}
 
public class test {
    public static void main(String[] args) {
        Dog dog = new Dog("张三",19,'男');
        dog.print();
 
    }
}

使用this调用其他构造方法的时候要放在第一行

四、代码块

4.1普通代码块

定义在方法中,使用{}括起来的代码

如果在括号外进行打印a,会报错因为出了普通代码块,a就不存在了,生命周期就是出了普通代码块。

public class hello {
    public static void main(String[] args) {
        {
            int a = 0;
        }
        System.out.println(a);//会报错
    }
 
 
}

4.2 构造代码块

定义在类中,使用{}括起来的代码块,直接定义在类中,不加任何修饰符的代码块,优先于构造方法执行,有几个对象产生就调用几次构造块。

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
    public Dog(String name,int age,char sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    {
        //构造代码块
        name = "曹操";
    }
    public Dog(int a){
        System.out.println("一个参数的有参构造");
    }
 
    public Dog(){
        System.out.println("我是无参构造");
    }
 
    void print(){
        System.out.println("姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex);
    }
 
}
 
public class test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.print();
 
    }
}

可以利用构造代码块对对象进行赋值,但是代码块在构造方法之前所以,构造方法的赋值可能会对代码块赋值进行覆盖.

4.3.静态代码块

静态代码块是定义在类中的,用static关键字修饰,在类加载的时候就执行,而且执行一次。

 

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
    public Dog(String name,int age,char sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    static {
        System.out.println("我是静态代码块");
    }
    public Dog(int a){
        System.out.println("一个参数的有参构造");
    }
 
    public Dog(){
        System.out.println("我是无参构造");
    }
 
    void print(){
        System.out.println("姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex);
    }
 
}
 
public class test {
    public static void main(String[] args) {
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();
        Dog dog3 = new Dog();
 
 
    }
}

 

五、 toString方法

我们先看一段代码

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
}
 
public class test {
    public static void main(String[] args) {
        Dog dog1 = new Dog();
 
        System.out.println(dog1);
 
    }
}

打印的是该对象的地址,我们可以改写toString的方法

package bao1;
 
class Dog {
    private String name;
    public int age;
    char sex;
 
    public Dog(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
 
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}
 
public class test {
    public static void main(String[] args) {
        Dog dog1 = new Dog("张三",18,'女');
 
        System.out.println(dog1.toString());
 
    }
}

目录
相关文章
|
存储 编译器 C语言
【C++】类和对象(上)
【C++】类和对象(上)
88 0
|
6月前
|
C++
C++ --> 类和对象(三)
C++ --> 类和对象(三)
46 9
|
6月前
|
存储 编译器 程序员
【C++】类和对象(上)
【C++】类和对象(上)
|
9月前
|
存储 Java 编译器
类和对象详解
类和对象详解
|
9月前
|
Java 编译器 Linux
C++:类和对象(下)
C++:类和对象(下)
56 1
|
9月前
|
编译器 C++
C++类和对象(下)
C++类和对象
87 0
|
9月前
|
存储 编译器 C语言
C++-类和对象(2)
C++-类和对象(2)
55 0
|
存储 编译器 C语言
类和对象(上)
类和对象(上)
81 0
|
安全 程序员 C++
C++ 类和对象(二)
C++ 类和对象(二)
43 0
|
存储 编译器 C++
【C++】类和对象(三)
类和对象(三) 拷贝构造函数: 当我们想要将一个已确定的类变量的值拷贝给另外一个相同类型的类变量,有什么快捷的方法吗?
45 0