Java object-oriented object small solution

What is an object?
Simply put, we ourselves are an object, a person.
An apple, a small fish, and a horse are all objects

Objects have their own characteristics, such as ourselves, with eyes and names, these are called attributes.
Objects have their own behaviors, such as ourselves, eating behaviors, and speaking behaviors. These are called methods
. Recall the categories:
people include men, women, the elderly and children, and pigs include wild boars and domestic pigs, but they all belong to the two categories
Insert picture description here
of humans and pigs. When it comes to objects, I have to review object
-oriented. What is object -oriented? What's the use?

This is to think about the purpose of the computer:

用计算机语言描述现实世界
用计算机解决现实中的问题

And facing-oriented is to help developers:

1. Communication is more fluent
2. Improve design and development efficiency
3. In line with human thinking habits
4. Improve design and development efficiency

Then come back to see how to use computer language to describe the class to which an object belongs and its attributes and methods

Representation
Insert picture description here
of the representation
Insert picture description here
method of the representation of the class
Insert picture description here
Code representation:

狗类
public class Dog {
    
    //狗的类名(Dog)
   狗的属性如下
    String name="小哈";      //昵称
    int health  = 60;        //健康值
    int love = 20;             //亲密度
    String strain = "哈士奇";      //品种

    /**
     * 狗的方法
     */
    public void print(){
    
    
        System.out.println(String.format("宠物的自白:\n我的名字叫%s,健康值是%d,和主人的亲密度是%d,我是一只%s",this.name,this.health,this.love,this.strain));
    }
}
===========
//创建狗狗对象
            Dog dog = new Dog();
            dog.name=name;
            dog.strain=strain;
            dog.print();//调用狗的方法

Note here a keyword and modifier:

static keyword

static可以修饰:变量,方法,代码块
static修饰的变量可以使用类名直接调用
static修饰的代码块自动加载,并且在第一行输出

Code example:

/**
 * 宠物企鹅类,使用静态常量。
 */
public class Penguin {
    
    
    String name = "无名氏"; // 昵称
    int health = 100; // 健康值
    int love = 0; // 亲密度
    static final String SEX_MALE ="Q仔";
    static final String SEX_FEMALE="Q妹";
    //static final String SEX_MALE = "雄";
    //static final String SEX_FEMALE = "雌";
    String sex = SEX_MALE; // 性别
    /**
     * 输出企鹅的信息。
     */
    public void print() {
    
    
        System.out.println("宠物的自白:\n我的名字叫" + this.name 
                + ",健康值是" + this.health     + ",和主人的亲密度是" 
                + this.love + ",性别是 " + this.sex + "。");
    }
}

测试类===========
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Penguin pgn = null;
        pgn = new Penguin();
        System.out.println("第一个企鹅的性别是" + pgn.sex + "。");

        pgn = new Penguin();
        pgn.sex = Penguin.SEX_FEMALE;
        System.out.println("第二个企鹅的性别是" + pgn.sex + "。");

        pgn = new Penguin();
        pgn.sex = Penguin.SEX_MALE;
        System.out.println("第三个企鹅的性别是" + pgn.sex + "。");
    }
}

final modifier

final修饰变量,变量便常量
final修饰方法,方法不能被重写
final修饰类,类不能被继承

Construction method and method overload

构造方法:

 - 方法名和类名相同,
 - 与访问修饰符和返回值类型无关
 - 分为有参构造和无参构造
 - 作用:可以给声明的属性赋值
 - 在没有声明构造方法的情况下,系统默认提供一个无参构造方法; 如果声明了构造方法,系统则不会提供默认的无参构造方法

方法重载:

在同一个类中

 - 方法名相同
 - 参数的列表不同{
    
    参数的个数不同,参数的顺序不同,参数的类型不同}
 - 与访问修饰符和返回值类型无关
 - 可以是普通方法构成,也可以是构造方法构成

Look at the code:

/**
 * 狗类
 */
public class Dog {
    
    
    String name="小哈";      //昵称
    int health  = 100;        //健康值
    int love = 0;             //亲密度
    String strain = "哈士奇";      //品种

    /**
     * 无参构造
     */
    public Dog(){
    
    
        System.out.println("执行了无参构造");
    }
    /**
     * 有参构造
     * @param name      昵称
     * @param strain    品种
     */
    public Dog(String name,String strain){
    
    
        System.out.println("执行了有参构造");
        this.name=name;
        this.strain=strain;
    }
    /**
     * 宠物的自述
     */
    public void print(){
    
    
        System.out.println(String.format("宠物的自白:\n我的名字叫%s,健康值是%d,和主人的亲密度是%d,我是一只%s",this.name,this.health,this.love,this.strain));
    }
}
=====================================
测试类:

public class Test {
    
    
    public static void main(String[] args){
    
    
        //创建狗狗对象
        Dog dog = new Dog();        //默认调用无参构造
        dog.print();
        System.out.println();
        //调用有参构造
        dog = new Dog("旺财","泰迪");	//调用有参构造
        dog.print();
        System.out.println();
    }
}

Use of this
1) What does this stand for? : Represents the current class object (who calls this method, this refers to whom)
2) What is the difference between adding this and not adding this to an attribute in a method?
this. attribute: a certain specified attribute.
Variable name (attribute name is the same): first check whether there is a variable or parameter with the same name in this method. The variable refers to
the variable or parameter. If there is no parameter or variable with the same name in the method, then Refers to the attribute with the same name, otherwise
an error will be reported

code show as below:

public class Dog{
    
    
    public  Dog() {
    
    
        System.out.println("狗出生了---顺产");
    }
    //自己添加的构造方法
    public   Dog(String name,String strain) {
    
    
        this.name = name;
        this.strain = strain;
        System.out.println("狗出生了...---难产.");
    }
    
    public Dog(String a,int b) {
    
    
       System.out.println("狗出生了----剖腹产");
    }
    public Dog(int b,String a) {
    
    
        System.out.println("狗出生了----流产出来了");
    }
    
    String name = "无名氏"; // 昵称,默认值是"无名氏"
    int health = 100; // 健康值,,默认值是100
    int love = 0; // 亲密度
    String strain = "哈士奇"; // 品种
    /**
     * 输出狗狗的信息。
     */
    public void print() {
    
    
        System.out.println("宠物的自我介绍:\n我的名字叫" + this.name +
                ",健康值是" + this.health + ",和主人的亲密度是"
                + this.love + ",我是一只 " + this.strain + "。");
    }
}

Guess you like

Origin blog.csdn.net/weixin_52841956/article/details/114132935