多个值的类继承 [英] Class Inheritance for multiple values

查看:89
本文介绍了多个值的类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我上课,请说:

If I had a class say:

 class Car
    {
        private string make, model;
        private string Cars(string make, string model)
        {
            make = make + model;
            return make;
        }
        public string MyProperty
        { 
            get { return make; }
            set { make = Cars(value, value); } 
        {
}
 class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            Console.WriteLine("What is the Make of the car: ");
            car.MyProperty = Console.ReadLine();
            Console.WriteLine("What is the Model of the car: ");
            car.MyProperty = Console.ReadLine();
            Console.WriteLine("The Make of the car is: {0}\nAnd the Model of the car                  is: {0}", car.MyProperty);
            Console.ReadLine();
        }
    }



我该如何使用属性将2个值连接到字符串并正确输出.



How can I get 2 value with my property concat the strings and have it output properly.

推荐答案

首先,我将make和model设置为单独的属性:

First, I would make the make and model separate properties:

public string Make { get; set; }
public string Model { get; set; }


0)您可以重写ToString()方法以返回包含所需信息的格式字符串.


0) You could override the ToString() method to return a frmatted string containing the info you wanted.

public override string ToString()
{
    return string.Format("{0} {1}", this.Make, this.Model);
}



并像这样使用它:



And use it like this:

Console.WriteLine(car.ToString());
// or like this:
Console.WriteLine(car);



1)您可以编写一种方法来返回所需的格式化信息.



1) You could write a method to return the formatted info you wanted.

public string GetMakeAndModel()
{
    return string.Format("{0} {1}", this.Make, this.Model);
}



并像这样使用它:



And use it like this:

Console.WriteLine(car.GetMakeAndModel());


public class Car
   {
   public string Make { get; set; }
   public string Model {get; set;}
   public string Car(string make, string model)
      {
      Make = make;
      Model = model;
      }
   public override string ToString()
      {
      return string.Format("{0}:{1}", Make, Model);
      }
   }
class Program
   {
   static void Main(string[] args)
      {
      Console.WriteLine("What is the Make of the car: ");
      string make = Console.ReadLine();
      Console.WriteLine("What is the Model of the car: ");
      string model = Console.ReadLine();
      Car car = new Car(make, model);
      Console.WriteLine("The Make of the car is: {0}\nAnd the Model of the car is: {1}", car.Make, car.Model);
      Console.WriteLine("The car is {0}", car);
      Console.ReadLine();
      }
   }




它起作用了,但是我不明白的是" Console.WriteLine(您的汽车:{0}",汽车);知道从何处获取格式化的字符串.因为"公共重写字符串ToString()''没有涉及其中的任何类型的标识符,以表明其要完成的工作的所有权."


如果您将任何变量输入到string.Format方法中(而Console.WriteLine就是这么做的-它会将您的参数输入到string.Format中,并将结果打印到控制台中),那么它将隐式调用yourVariable.ToString()以便得到可以打印的内容-如果变量已经不是字符串.
上面的ToString方法访问Car类的当前实例的两个属性(也称为this)并获取相关数据.可以将其重写为:




"It worked, but what I didn''t understand is how ''Console.WriteLine("Your Car: {0}", car);'' knew where to get the formatted string. Because ''public override string ToString()'' doesn''t have any type of identifier involved in it to show ownership of what its trying to accomplish."


If you feed any variable into a string.Format method (and Console.WriteLine does just that - it feeds your parameters into string.Format and prints the result to the console) then it does an implicit yourVariable.ToString() call in order to get something it can print - if the variable isn''t a string already.
The ToString method above accesses the two properties of the current instance of the Car class (also known as this) and fetches the relevant data. It could be re-written as:

public override string ToString()
   {
   return string.Format("{0}:{1}", this.Make, this.Model);
   }

但是这个".所有非静态方法中都隐含了bit,因此可以省略以提高可读性.通常,只有在方法参数与属性或字段之间存在名称重复的情况下,我才包括它.

But the "this." bit is implied in all non-static methods, so can be left out for readability. Normally, I only include it if there is a duplication of names between a method parameter and a property or field.

Does that make sense?


您将需要具有两个组件的结构化数据类型,struct或数组的属性.

但这真的值得并发症吗?


或者:
You will need a property of a structured data type, struct or an array, with two components.

But is it really worth the complication ???


Alternatively:
public string MyProperty
{
    get { return string.Concat(make, '|', model); }
    set { make = value.Split('|')[0]; model = value.Split('|')[1]; }
}



设置器的代码向您展示如何检索字符串部分.



The code of the setter shows you how to retrieve the string parts.


这篇关于多个值的类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆