简体   繁体   English

超级 class 方法的 Inheritance 方法无法获取子类中的详细信息?

[英]Inheritance method of super class method can't get the details in subclass?

I have three classes Person class,Main class and Donar class,我有三类人 class,主要 class 和 Donar class,

Person class人 class

public class Person{
    
private String name;
    private String  dateOfBirth;
    private String  gender;
    private String  mobileNumber;
    private String  bloodGroup;
    
    Person(){
        
    }
    
     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public String getBloodGroup() {
        return bloodGroup;
    }

    public void setBloodGroup(String bloodGroup) {
        this.bloodGroup = bloodGroup;
    }
     
    public void displayPersonDetails( )
    {
        System.out.println("Name : " + name);
        System.out.println("Date of Birth : " +dateOfBirth);
        System.out.println("Gender : " +gender);
        System.out.println("Mobile Niumber : " +mobileNumber);
        System.out.println("Blood Group : "+bloodGroup);
    }

}

Donor class捐赠者class

class Donor extends Person
{
    
    
   private String bloodBankName;
    private String  donorType;
    private String  donationDate;
    
    public Donor() {
    }

    public String getBloodBankName() {
        return bloodBankName;
    }

    public void setBloodBankName(String bloodBankName) 
    {
        this.bloodBankName = bloodBankName;
    }

    public String getDonorType() {
        return donorType;
    }

    public void setDonorType(String donorType) {
        this.donorType = donorType;
    }

    public String getDonationDate() {
        return donationDate;
    }

    public void setDonationDate(String donationDate) {
        this.donationDate = donationDate;
    }
    
    public void displayDonationDetails( ) {             
        System.out.println("Donation Details :");
        super.displayPersonDetails();
        System.out.println("Blood Bank Name : "+bloodBankName);
        System.out.println("Donor Type : "+donorType);
        System.out.println("Donation Date : "+donationDate);
        
    }
    
} 

Main class主class

package Eboxex1;

import java.util.Scanner;

public class Main { 
    public static void main(String[] args){ 
      Donor d=new Donor();
       Person p=new Person();
    Scanner sc=new Scanner(System.in);
    
    
    System.out.println("Enter the name :");
    p.setName(sc.nextLine());
            
    System.out.println("Enter Date of Birth :");
    p.setDateOfBirth(sc.nextLine());
    System.out.println("Enter Gender :");
    p.setGender(sc.nextLine());
    
    System.out.println("Enter Mobile Number :");
    p.setMobileNumber(sc.nextLine());
    
    System.out.println("Enter Blood Group :");
    p.setBloodGroup(sc.nextLine());
    
    System.out.println("Enter Blood Bank Name :");
    
 d.setBloodBankName(sc.nextLine());
    
    System.out.println("Enter Donor Type :");
    
    d.setDonorType(sc.nextLine());
    
    System.out.println("Enter Donation Date :");
    
    d.setDonationDate(sc.nextLine());
    
    d.displayDonationDetails(); 
   
    sc.close();
    } 
}

Output Output

输出

I don't know what was the error is.我不知道错误是什么。

when we run the main method means it will ask for the details from the user then details like Name, Date of Birth, Gender, Mobile Number, Blood Group are goes to the Person class (getter and setter method) then details like Blood Bank Name, Donor Type, Donation Date are goes to the Donor class (getter and setter method) Then the method displayDonationDetails are called in the main class.当我们运行 main 方法时,意味着它将询问用户的详细信息,然后诸如Name, Date of Birth, Gender, Mobile Number, Blood Group类的详细信息转到Person class(getter 和 setter 方法),然后是Blood Bank Name, Donor Type, Donation Date类的详细信息Blood Bank Name, Donor Type, Donation Date都转到Donor class (getter and setter 方法) 然后在主displayDonationDetails中调用方法 displayDonationDetails。 In this method, we have to call the details in the Person class.在此方法中,我们必须调用 Person class 中的详细信息。 but the method cannot fetch the details of the Person class to Donor class method但该方法无法将Person class 的详细信息获取到Donor class 方法

You are creating two seperate objects: Person "p" and Donor "d" (which due to inheritance is a Person itself).您正在创建两个单独的对象:人“p”和捐赠者“d”(由于 inheritance 本身就是一个人)。

Then you set all the person data (name, date of birth, gender etc.) on the "p" object and all the donor data (donor type, donation date etc.) on the "d" object.然后在“p”object 上设置所有个人数据(姓名、出生日期、性别等),在“d”object 上设置所有捐赠者数据(捐赠者类型、捐赠日期等)。

So at the end of the main-method your objects look like this:因此,在 main-method 结束时,您的对象如下所示:

Person "p":人“p”:

|---------------|---------------|
| Attribute     | Value         |
|---------------|---------------|
| name          | jano          |
| dateOfBirth   | 5/11/1998     |
| gender        | Female        |
| mobileNumber  | 456437538     |
| bloodGroup    | A+ve          |
|---------------|---------------|

Donor "d" (Donor inherits from Person class):捐赠者“d”(捐赠者继承自 Person 类):

|---------------|---------------|
| Attribute     | Value         |
|---------------|---------------|
| name          | null          |
| dateOfBirth   | null          |
| gender        | null          |
| mobileNumber  | null          |
| bloodGroup    | null          |
| bloodBankName | Bload assur e |
| donorType     | ssssssssssag  |
| donationDate  | 5/787/989     |
|---------------|---------------|

And then via the call to method "displayDonationDetails" you display only the data of Donor "d".然后通过调用方法“displayDonationDetails”,您只显示捐赠者“d”的数据。 So everything works as expected.所以一切都按预期工作。

Only create the Donor object "d" and set all values for it, then everything works as you expect it to do.只需创建 Donor object "d" 并为其设置所有值,然后一切都会按照您的预期进行。

You need just the Donor object for your whole program.您的整个项目只需要捐赠者 object。 You appear to be creating 2 object a person object p and a donor object d and calling the set methods on both those objects.您似乎正在创建 2 object 一个人 object p 和一个捐赠者 object d 并在这两个对象上调用 set 方法。 Use only the donor object d for your whole program and to display person details inside your donor class call super.displayPersonDetails() inside your display donationDetails() as the first line or better you can create a method caled displayDetails() in your person class and have your donor class overide that在您的整个程序中仅使用捐赠者 object d 并在您的捐赠者 class 中显示人员详细信息,在您的显示donationDetails() 中调用 super.displayPersonDetails() 作为第一行或更好的方法,您可以在您的人员 ZABB4F2DZC42 中创建一个名为 displayDetails() 的方法并让您的捐助者 class 覆盖

When you are using inheritence, you automatically get the properties of the parent class.使用继承时,会自动获取父 class 的属性。 So you only have to create child class object.所以你只需要创建子 class object。

Your main class should look like this: -您的主要 class 应如下所示:-

public class Main { 
    public static void main(String[] args){ 
      Donor d=new Donor();
      Scanner sc=new Scanner(System.in);
    
    
    System.out.println("Enter the name :");
    d.setName(sc.nextLine());
            
    System.out.println("Enter Date of Birth :");
    d.setDateOfBirth(sc.nextLine());
    System.out.println("Enter Gender :");
    d.setGender(sc.nextLine());
    
    System.out.println("Enter Mobile Number :");
    d.setMobileNumber(sc.nextLine());
    
    System.out.println("Enter Blood Group :");
    d.setBloodGroup(sc.nextLine());
    
    System.out.println("Enter Blood Bank Name :");
    
 d.setBloodBankName(sc.nextLine());
    
    System.out.println("Enter Donor Type :");
    
    d.setDonorType(sc.nextLine());
    
    System.out.println("Enter Donation Date :");
    
    d.setDonationDate(sc.nextLine());
    
    d.displayDonationDetails(); 
   
    sc.close();
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM