本文介绍了为什么构造函数上的继承不起作用?我怎样才能使它有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的初学者,我尝试使用构造函数进行继承,但似乎没有工作。



我有什么试过:



I'm a beginner in java and I try to work on inheritance using constructor, but it seems not to be working.

What I have tried:

import java.io.*;
public class Person {
    String name;
    String address;
    String tel_no;
    String answer;
  
  public Person(String name, String address, String tel_no, String answer) {
    this.name = name;
    this.address = address;
    this.tel_no = tel_no;
    this.answer = answer;
  }
}
class Bar {
    public static void main(String[] args) throws IOException {
    BufferedReader k=new BufferedReader(new InputStreamReader
            (System.in));
  String answer = "no";
do{
    System.out.print("Please enter name: ");
    String name=k.readLine( );  // local variable
    System.out.print("Please enter address: ");
    String address=k.readLine( );
    System.out.print("Please enter phone number: " ); // to handle the end of line characters
    String tel_no =k.readLine( );
    System.out.print("Is your information Correct? ");
    answer=k.readLine( );
    // use local variables in constructor call
    Person person = new Person(name, address, tel_no, answer);    
} while(answer.equals("no"));
}

//subclass
import java.io.*;
public class Customer extends Person{
    String ID;
  
  public Customer(String ID) {
    this.ID = ID;
  }
}
class Geren {
    public static void main(String[] args) throws IOException {
    BufferedReader k=new BufferedReader(new InputStreamReader
            (System.in));
  String answer = "no";
    do{
    System.out.print("Please enter your ID number: ");
    String ID=k.readLine( );
    Customer cust = new Customer(ID);    
    } while(answer.equals("no"));
  }
}

推荐答案

public class Customer extends Person{
    String ID;
    
    public Customer(String ID, String name, String address, String tel_no, String answer) {
        super(name, address, tel_no, answer);
        this.ID = ID;
    }
}



[]


这篇关于为什么构造函数上的继承不起作用?我怎样才能使它有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:57