我知道我的基本程序应该是什么样子,我将非常感谢你的帮助:
using System;namespace Introduction_to_classes{ class Person { int Age; string name; int DateOfBirth() { return 2013 - Age; } } class Program { public static voID Main() { Person Mother = new Person(35,Alice); Person Son = new Person(12,Johny); Mother.name = "Lucy"; // Just changing the value afterwards if(Mother.Age > Son.Age) { int year = Mother.DateOfBirth(); Console.Writeline("Mom was born in {0}.",year); } Console.Readline(); } }}
这只是一个想法,它绝对不起作用.但最重要的是,如果你能够将其改正为工作实例,它将对我有所帮助……
解决方法class Person{ public int Age { get; set; } public string name { get; set; } public Person(int age,string name) { Age = age; name = name; } public int DateOfBirth() { return 2013 - Age; }} class Program { public static voID Main() { Person Mother = new Person(35,"Alice"); Person Son = new Person(12,"Johny"); Mother.name = "Lucy"; // Just changing the value afterwards if (Mother.Age > Son.Age) { int year = Mother.DateOfBirth(); Console.Writeline("Mom was born in {0}.",year); } } }
一些有用的链接:properties,constructor
总结以上是内存溢出为你收集整理的C#类 – 基本示例全部内容,希望文章能够帮你解决C#类 – 基本示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)