通过程序将变量和函数添加到类中 [英] Adding variables and functions to class through program

查看:92
本文介绍了通过程序将变量和函数添加到类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图编写一个程序来排序和列出一些数据。我在课堂上使用了一个向量,所以每个条目都有自己的对象。但我意识到我可能想为每个条目添加更多标签/分类并存储这些新数据我需要在我的班级中使用新变量或新函数。如何在我的课程中添加类似变量和Getter + Setter的内容,而不必知道我要添加哪些新类别?



我尝试过的:



我试图用构造函数做一些事情,但我认为这不对。我使用数组而不是矢量。

解决方案

我认为你应该用 UML 以阐明您想要编码的内容。这是教程。一个简单的解决方案也是在纸上绘制 - 最好用铅笔。



不知道变量的类型是不好的风格。



你可以通过添加一个包含密钥(你的var)和一个值(值)的字典来完成这些工作。



常见的设计模式是接口。你定义了一些兼容的功能,但没有定义类。



来自经验丰富的开发人员的建议:如果你不知道实施你的工作是垃圾桶。 ; - )

So i am trying to code a program to sort and list some data. I use a vector with my class so every entry got its own object. But i realized i may want to add more tags/categorys to every entry and to store this new data i need a new variables or new functions in my class. How can i add something like variables and Getter + Setter to my class without having to know which new categorys i am going to add?

What I have tried:

I tried to do something with the constructor but i think thats not right. I used an array instead of vector.

解决方案

I think you should do some design wor with UML to clarify what you want to code. Here is a tutorial. An easy solution is also to draw it at paper - best with a pencil.

Dont knowing the type of the variables is bad style.

You can do such stuff by adding a dictionary which has a key (your var) and a value (the value).

A common design pattern is interface. You define some compatible functions but not the class.

An advice from a seasoned developer: if you dont know WHAT to implement your work is for the trashcan. ;-)


这篇关于通过程序将变量和函数添加到类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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