C#抽象方法,只读属性

时间:2018-05-07 16:54:25

标签: c# .net oop inheritance properties

我有一个任务(一堆oop东西,多态和继承),除此之外我还要做以下事情:

  1. 我需要在类Vehicle (称为 calculateOccupancy())中添加一个抽象方法,该方法必须返回车辆剩余空间的百分比。然后我必须在我的派生类中实现它。这里的问题是,我有3个派生类,其中两个有2个属性,一个有3个。那么如何创建我的抽象方法,以便它可以接受2个或3个参数。

  2. 我必须向类Person 添加一个不可更改的属性,并且该属性必须返回名称和姓氏的第一个字母,除以点。

    < / LI>

    namespace Example
    {
        abstract class Vehicle
        { 
            //class member variables, most likely unnecessary for the questions
            private Person driver;
            private string vehicleBrand;
            private string vehicleType;
            private double fuelConsumption;
            private double gasTankSize;
            private string fuelType;
    
            //the default constructor
            public Vehicle()
            {}
    
            //The abstract method from question 2
            // how to make it so that it wont error when I need to 
            //put in 3 variables instead of two, meaning, how would I add int c
            public abstract double calculateOccupancy (int a, int b);
    
            //The derived class that implements the method
            class Bus : Vehicle
            {
                private int allSeats; 
                private int allStandingSeats; 
                private int busPassengers; //the number of passengers
    
                //the constructor
                public Bus (int a, int b, int c)
                {
                    allSeats=a;
                    allStandingSeats=b;
                    busPassengers=c;
                }
    
                //the abstract method 
                // needs to take in int b (standing seats)
                public override double calculateOccupancy(int a, int c)
                {
                     //this code calculates the leftover space in the vehicle
                     double busSpace=(busPassengers*100) / allSeats;
                     return busSpace;
    
                     //same code for the leftover standing space (int c)
                } 
            }
        }
    
        class Person
        {
            protected string name;
            protected string lastName;
            //question 1
            //properties for char gender
            protected char gender;
            //question 3
            protected readonly string initials;
    
            //the code errors, at the get/set
            public char Gender
            {
                get{ return gender; }
                set {gender=value;}
            }
    
            /*and the property im not sure how to make
            public string Initials{}
            */
        }
    

    我希望这些评论能够增加一些清晰度,而不是混淆,谢谢你们的帮助。

2 个答案:

答案 0 :(得分:0)

提供&#34;可选&#34;创建抽象方法时值一个值

False

答案 1 :(得分:0)

假设继续 - 我将一些变量名称扔进谷歌翻译,它似乎是斯洛文尼亚语。我假设前进有助于我清楚地了解你的代码所做的事情。

1)替换 - 如果您已经有一个代表char的{​​{1}}变量,那么我相信您应该使用新的{{1}您要创建以表示它的类型。

spol

变化:

enum

收件人:public enum Spol { Moski = 0, Zenska = 1 }

2)默认&amp;条件 - 使用protected char spol; public char Spol { get{ return spol; } set {spol=value;} } 作为第3个参数,并使用忽略它的公式/算法(如果它是默认值)。

3)吸气剂 - 此属性没有设置器,因此无法直接更改。

public Spol Spol { get; set; }

备注

1)强烈建议将容量函数的参数(即int c = 0)命名为有用的东西(即描述其内容的名称),而不是public string GiveThisAName { get { if (String.IsNullOrWhiteSpace(ime)) { return null; } if (String.IsNullOrWhiteSpace(priimek)) { return null; } return ime[0] + '.' + priimek[0]; } } izracunajZasedenost(int a, int b)

2)对于记录,#1似乎更适合您的教师,老师或任何给您这项任务的人提出的适当问题。