利用java抽象现实中的一个物体

利用java抽象现实中的一个物体并写出三个方法,其中一个是重载方法,一个是构造方法。

除了学生之外其他的还有嘛…

很多的;简单举个例:

 public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }
    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() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }


}

import java.io.Serializable;
import java.time.LocalDateTime;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

/**

  • AppInfoEntity */

public class AppInfoPO implements Serializable {

private static final long serialVersionUID = 1L;

private String id;

private Integer animal; // animal

private String appType; // app_type

private String appName; // 应用名称

private String appGroup; // 应用分组

private LocalDateTime addTime; // 应用添加时间

private String appDesc; // 应用描述

private String createdBy;

private String updatedBy;

private LocalDateTime createdAt; // created_at

private LocalDateTime updatedAt; // updated_at

/**
 * 默认构建函数
 */
public AppInfoPO() {

}

/**
 * 构建函数
 */
public AppInfoPO(String id) {
    this.id = id;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Integer getAnimal() {
    return animal;
}

public void setAnimal(Integer animal) {
    this.animal = animal;
}

public String getAppType() {
    return appType;
}

public void setAppType(String appType) {
    this.appType = appType;
}

public String getAppName() {
    return appName;
}

public void setAppName(String appName) {
    this.appName = appName;
}

public String getAppGroup() {
    return appGroup;
}

public void setAppGroup(String appGroup) {
    this.appGroup = appGroup;
}

public LocalDateTime getAddTime() {
    return addTime;
}

public void setAddTime(LocalDateTime addTime) {
    this.addTime = addTime;
}

public String getAppDesc() {
    return appDesc;
}

public void setAppDesc(String appDesc) {
    this.appDesc = appDesc;
}

public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public LocalDateTime getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
    this.createdAt = createdAt;
}

public LocalDateTime getUpdatedAt() {
    return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
    this.updatedAt = updatedAt;
}

@Override
public int hashCode() {
    return Objects.hashCode(super.hashCode(), animal, appType, appName, appGroup, addTime, appDesc, createdBy,
            updatedBy, createdAt, updatedAt);
}

@Override
public boolean equals(Object object) {
    if (object instanceof AppInfoPO) {
        if (!super.equals(object))
            return false;
        AppInfoPO that = (AppInfoPO) object;
        return Objects.equal(this.animal, that.animal) && Objects.equal(this.appType, that.appType)
                && Objects.equal(this.appName, that.appName) && Objects.equal(this.appGroup, that.appGroup)
                && Objects.equal(this.addTime, that.addTime) && Objects.equal(this.appDesc, that.appDesc)
                && Objects.equal(this.createdBy, that.createdBy) && Objects.equal(this.updatedBy, that.updatedBy)
                && Objects.equal(this.createdAt, that.createdAt) && Objects.equal(this.updatedAt, that.updatedAt);
    }
    return false;
}

@Override
public String toString() {
    return MoreObjects.toStringHelper(this).add("super", super.toString()).add("animal", animal)
            .add("appType", appType).add("appName", appName).add("appGroup", appGroup).add("addTime", addTime)
            .add("appDesc", appDesc).add("createdBy", createdBy).add("updatedBy", updatedBy)
            .add("createdAt", createdAt).add("updatedAt", updatedAt).toString();
}

}

您好,您的问题很明确,学习Java这门面向对象的语言,就是要把万物都抽象为对象,比如说车,房子,面包等等。
所有的事物都可以抽象为对象,那么对应Java就是一个类,前面的朋友写的student类就是一种抽象,他声明了两个属性:一个是名字,一个是年龄
和一些方法,那么,你是否会怀疑:既然是一种学生的抽象,那么他能代表任意一个学生吗,答案是不行的,因为每个学生都有自己的特点,这么一个类是不能完美的表示学生的,所以你可以理解这样的一个学生类,他抽象出的是学生的共同特征(就是属性,如上面的名字,年龄,当然如果您需要,也可以加上性别,身高等),当然还有一些行为(也就是方法),比如在学生上都需要执行的一些操作,如获取学生姓名,获取分数等,是每个学生都需要的操作,那么这些都需要的操作也会书写到当前类中作为方法。