简体   繁体   中英

C# Abstract methods, readonly properties

I have an assignment (bunch of oop stuff, polymorphism and inheritance) and amongst other things I have to do the following:

  1. I need to add an abstract method to the class Vehicle (called calculateOccupancy() ) which has to return the % of the leftover space in a vehicle. I then have to implement that in my derived classes. The issue here is, I have 3 derived classes, two of them have 2 attributes and one has 3. So how do I make my abstract method, so that it can accept 2 or 3 arguments.

  2. I have to add a unchangeable property to the class Person , and the property has to return the first letter of the name and surname, divided by a dot.

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{}
        */
    }

I hope the comments add some clarity, rather than confusion, thank you for your help everybody.

Give the "optional" values a value when you create the abstract method

public abstract double izracunajZasedenost (int a = -1, int b = -1) 
{
    if (a == -1){ 
        //do method with ignoring a
    }
};

Assumption going forward - I threw some of your variable names into Google Translate and it seems to be Slovenian. I'm assuming that going forward which helped me make some clarity of what your code does.

1) Replace - If you already have a variable that is a char representing spol then I believe you're supposed to use the new enum type you are to create to represent it.

public enum Spol
{
    Moski = 0,
    Zenska = 1
}

Change:

protected char spol;
public char Spol
{
    get{ return spol; }
    set {spol=value;}
}

To: public Spol Spol { get; set; } public Spol Spol { get; set; }

2) Defaults & Conditions - Use int c = 0 as your 3rd parameter and use a formula/algorithm that ignores it if it is the default value.

3) Getters - This property doesn't have a setter and therefore cannot be changed (directly).

public string GiveThisAName
{
    get
    {
        if (String.IsNullOrWhiteSpace(ime))
        {
            return null;
        }

        if (String.IsNullOrWhiteSpace(priimek))
        {
            return null;
        }

        return ime[0] + '.' + priimek[0];
    }
}

Notes

1) Heavily recommend making the parameters of your capacity function (ie izracunajZasedenost(int a, int b) ) to be named something useful (ie a name descriptive of what they do) other than a and b .

2) For the record, #1 seems more like an appropriate question for your instructor, teacher, or whoever gave you this assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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