简体   繁体   English

可以像这样使用单个类吗?

[英]Can a single class be used like this?

I have data to be stored to a file.我有数据要存储到文件中。 The data is as below:数据如下:

    {
       "student": {
                      "FirstName": {
                                     "Initial": "Mr.",
                                     "Value": "Luis"
                                   },
                     "MiddleName": {
                                     "Initial": "Mr.",
                                     "Value": "Bruce"
                                   },
                       "LastName": "Wayne"
                 }
    }

I retrieve the data from the file into a class Student:我将文件中的数据检索到 Student 类中:

     public class Student
    {
        public FirstName Firstname {get;set;}
        public MiddleName Middlename {get;set;}
        public string LastName {get;set;}
    }

    public class FirstName
   {
       public string Initial {get;set;}
       public string Value {get;set;}
   }
    public class MiddleName
   {
       public string Initial {get;set;}
       public string Value {get;set;}
   }

My question is: Can I build a class as given below to store the same info?我的问题是:我可以建立一个如下所示的类来存储相同的信息吗?

     public class Student
    {
        public NameClass Firstname {get;set;}
        public NameClass Middlename {get;set;}
        public string LastName {get;set;}
    }

    public class NameClass
   {
       public string Initial {get;set;}
       public string Value {get;set;}
   }

Is it the right approach?这是正确的方法吗? or my previous approach is better?还是我以前的方法更好?

Don't see any problem, with that.看不出有什么问题,用那个。

Actually it's definitely better, then was done before, as you unify common information in one single class.实际上它肯定比以前更好,因为您将公共信息统一在一个类中。

The only thing, that comes to my mind, is that you have to revise your serialization to and from ,as you change classes architecture.唯一的一点,那在我脑海中,是你有你的序列化修改tofrom ,当您更改类的建筑。

Yeah, it works pretty well...Following class structure is optimized是的,效果很好……优化了下面的类结构

public class Student
 {
    public NameClass Firstname {get;set;}
    public NameClass Middlename {get;set;}
    public string LastName {get;set;}
 }

 public class NameClass
{
   public string Initial {get;set;}
   public string Value {get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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