2014-11-10 3 views
-3

고객 및 거래라는 두 가지 클래스가 있습니다. 고객을 거래로 확장하려고 할 때 오류가 발생합니다. 누구든지 도와 줄 수 있습니까? 이 당신은 Customer 생성자의 동일한 매개 변수 Transactions 클래스의 생성자를 정의해야 하나, 또는 Customer 클래스에 매개 변수가없는 생성자를 추가해야 내 코드클래스의 생성자를 지정된 유형에 적용 할 수 없습니다.

public class Customer{ 

    public String name; 
    public String surname; 
    public int id; 
    public int ban; 
    public double balance; 


    public Customer(String pName, String pSurname, int pId, int pBan, double pBalance) 
    { 
     name = pName; 
     surname = pSurname; 
     id = pId; 
     ban = pBan; 
     balance = pBalance; 
    } 

} 

public class Transactions extends Customer{ 

    public double Deposit(){ 
     double deposit = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to deposit?")); 
     balance = balance + deposit; 
     JOptionPane.showMessageDialog(null,"Transaction complete. Your new balance is " + balance,"Transaction Complete", JOptionPane.PLAIN_MESSAGE); 
     return balance; 

    } 

    public double Withdraw(){ 
     double withdraw = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to withdraw?")); 
     if(balance >= withdraw){ 
      balance = balance - withdraw; 
      JOptionPane.showMessageDialog(null,"Transaction complete. Your new balance is " + balance,"Transaction Complete", JOptionPane.PLAIN_MESSAGE); 
      } 
     else{ 
      JOptionPane.showMessageDialog(null,"Error. You were trying to withdraw more than you have in your account.","ERROR", JOptionPane.ERROR_MESSAGE); 
     } 
     return balance; 
    } 

    public void checkBalance(){ 
     JOptionPane.showMessageDialog(null, "Name: " + name + " " + surname + "\nID: " + id + "\nBAN: " + ban + "\nBalance: " + balance); 
    } 
+0

코드 제대로 포맷. 어떤 오류가 발생하고 있습니까? 너 뭐 해봤 니? – Taylor

+0

고객 클래스의 생성자 고객은 주어진 유형에 적용 할 수 없습니다. –

+0

Transactions에는 생성자가없고 일부 인수가있는 생성자가있는 Customer 클래스를 확장하는 것처럼 보입니다. 따라서 Transactions에 대한 생성자를 추가하고 Customer 생성자를 호출하십시오. – Bharath

답변

0

입니다. (당신의 Transactions 클래스하지 않는 한) 클래스가 어떤 생성자가없는

, 컴파일러는 매개 변수가없는 기본 생성자를 생성합니다. 이 생성자는 수퍼 클래스의 매개 변수가없는 생성자를 호출합니다. 슈퍼 클래스에 매개 변수가없는 생성자가 없으면 (이미 다른 생성자가 있으므로 Customer 클래스 용으로 생성되지 않음) 코드가 컴파일되지 않습니다.

0

당신은 클래스 Transactions

그것은 당신이 당신의 자신의 생성자

0

당신은 상속/캡슐화 메커니즘을 돌파을 정의하기 때문에 존재하지 않는 슈퍼 클래스의 암시 적 생성자를 부르고위한 생성자를 정의하지 않았습니다.

  1. 모든 클래스의 멤버는 "private"이어야합니다.
  2. 일부 멤버를 하위 클래스가 액세스 할 수있게하려면 해당 멤버를 "보호 된"것으로 선언해야합니다.
  3. 하위 클래스에서 super 키워드를 통해 수퍼 클래스 생성자를 호출해야하는 생성자를 선언해야합니다. 아래 예를 확인하십시오.

    public class Person { 
        protected String name; 
        protected String age; 
        public Person(String name, String age) { 
         this.name = name; 
         this.age = age; 
        } 
    } 
    
    
    public class Student extends Person { 
        private String Level; 
        public Student(String name, String age, String level){ 
         super(name, age); 
         this.level = level; 
        } 
    } 
    
관련 문제