using System;
using System.Collections.Generic;
using System.IO;
abstract class Book
{
protected String title;
protected String author;
public Book(String t,String a){
title=t;
author=a;
}
public abstract void display();
}
class MyBook : Book
{
private int price = 0;
public MyBook(string t, string a, int p)
{
base.title = t;
base.author = a;
this.price = p;
}
public override void display()
{
Console.WriteLine($"Title: {title}");
Console.WriteLine($"Author: {author}");
Console.WriteLine($"Price: {price}");
}
}
나는 기본 키워드를 어떻게 사용하고 있는지에 대한 문제를 좁혔다. 기본 생성자 initializer를 생성자 선언에 base 키워드와 함께 사용해야합니다.
public MyBook(string t, string a, int p)
: base(t, a)
{
this.price = p;
}
누군가 내가 왜 자료를 사용할 수 없는지 설명해 주시겠습니까? 여기 내 생성자에? 제목과 저자 변수가 둘 다 추상 클래스의 보호 된 멤버이기 때문입니까? 나는 Book에서 파생 된 이래로이 보호받는 멤버들에게 접근 할 수 있다고 생각했습니다. 아니면 하위 클래스를 인스턴스화하지 않았기 때문에 내 생성자를 이런 식으로 설정할 수 없기 때문입니까?
같은 일에 대해 갈 것 같은 클래스 구조를 정의하세요. – vendettamit