gpt4 book ai didi

java - 基类和子类构造函数问题

转载 作者:行者123 更新时间:2023-11-29 08:16:23 25 4
gpt4 key购买 nike

过去几天,我一直在尝试使用 Java 中的类,从 youtube 上的“TheNewBoston”和 java 文档中了解它们。

我创建了以下场景,并就我的几个问题寻求你们(也包括女孩)的专业批评和深入了解。

有两个类person和person_financial,分别是基类和子类。

人类:

public class person {

private String name;
private String sex;
private int age;
private double height;
private double weight;
private double intelligence;

// person constructor arguments order: name, height, weight, age, sex, intelligence
public person(){
this("noname",0,0,0,"undefined",5);
}
public person(String n){
this(n,0,0,0,"undefined",5);
}
public person(String n, double h){
this(n,h,0,0,"undefined",5);
}
public person(String n, double h, double w){
this(n,h,w,0,"undefined",5);
}
public person(String n, double h, double w, int a){
this(n,h,w,a,"undefined",5);
}
public person(String n, double h, double w, int a, String s){
this(n, h, w, a, filterSex(s), 5);
}
public person(String n, double h, double w, int a, String s, double i){
name = n;
height = h;
weight = w;
age = a;
sex = filterSex(s);
intelligence = i;
}

public void setName(String n){
name = n;
}
public void setHeight(double h){
height = h;
}
public void setWeight(double w){
weight = w;
}
public void setAge(int a){
age = a;
}
public void setSex(String s){
sex = filterSex(s);
}
public void setIntel(double i){
intelligence = i;
}

public String getName(){
return name;
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public int getAge(){
return age;
}
public String getSex(){
return sex;
}
public double getIntel(){
return intelligence;
}

public String getInfo(){
return String.format("Name: %s,\nSex: %s,\nAge: %d,\nIntelligence: %.2f,"
+ "\nHeight: %.2f,\nWeight: %.2f\n", name, sex, age,
intelligence, height, weight);
}

private static String filterSex(String s){
return ((s.equalsIgnoreCase("male") ||
s.equalsIgnoreCase("female")) ? s : "undefined");
}
}

person_financial 类:

public class person_financial extends person {

private double monies = 0;

public void definePerson(String n, int a, String s, double i, double h, double w){
setName(n);
setAge(a);
setSex(s);
setIntel(i);
setHeight(h);
setWeight(w);
}

public person_financial() {
this(0);
}

public person_financial(double m) {
monies = m;
}

public void depositMonies(double m) {
monies += m;
}

public void withdrawlMonies(double m) {
if (m <= monies) {
monies -= m;
}
}

public double getBalance() {
return monies;
}
}

然后在主类中我有这个:

public class Main {

public static void main(String[] args) {

person p1 = new person("I have no Name", 180, 72, 38, "Alien", 7.2);
System.out.println(p1.getName());


person_financial pf1 = new person_financial(100.00);
pf1.depositMonies(50.02);
System.out.printf("%s has %.2f monies.\n", pf1.getName(), pf1.getBalance());

pf1.definePerson("some_name", 42, "male", 10, 180, 72);
System.out.println(pf1.getInfo());
}
}

在 person_financial 类中,我创建了一个名为“definePerson()”的方法,我用它来定义所有特征,否则这些特征将由“person”类的“person()”构造函数定义。我确信有一种更专业的方法可以从子类中为基类中的变量赋值,我只是不知道...

另外,有没有办法从“person”类中调用构造函数来定义“pf1”的特征?而不是必须,例如,手动设置每个属性,即 pf1.setName("something"); , 或 pf1.setAge(1000000);等等...或者有一个方法为我做这件事,如“definePerson()”。

非常感谢任何帮助,谢谢=)。

最佳答案

您使用 super() call 调用父类的构造函数。它必须是派生类的构造函数中的第一个调用,但是您像调用任何其他函数一样调用它(并传入参数),它会以这种方式调用构造函数。

关于java - 基类和子类构造函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com