C#控制台程序Person类中各个成员应满足以下要求: (1)数据成员:name、gender、yearOfBirth,分别对应人的姓名、性别、出生年份 (

qq_45044565 2019-11-24 09:00:11
C#控制台程序Person类中各个成员应满足以下要求: (1)数据成员:name、gender、yearOfBirth,分别对应人的姓名、性别、出生年份 (2)属性:Myname、MyGender、MyYearOfBirth和Age,其中属性Myname、MyGender、MyYearOfBirth对应数据成员name、gender、yearOfBirth,Age为只读属性,通过yearOfBirth和当前年份计算(提示:DateTime.Now.Year返回系统设定的年份)。 (3)构造函数:Person,实现对每个成员赋初值 (4)方法:Print(),输出人的姓名、称谓和年龄,男性请称呼先生,女性请称呼女士。
...全文
645 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_45044565 2019-11-24
  • 打赏
  • 举报
回复
引用 1 楼 qq_45044565的回复:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("一只" + name + "的" + gender + "正在以" + yearOfBirth + "Km/h的速度在奔跑\n"); Console.ReadLine(); } public class Person { private string name; private DateTime yearOfBirth; private string gender; public Person(string name, DateTime birthday, string gender) { this.name = name; this.yearOfBirth = yearOfBirth; this.gender = gender; } public string Myname { get { return this.name; } set { this.name = value; } } public string Age { get { return this.name; }//只读 } public string MyGender { get { return this.gender; } set { this.gender = value; } } } public int GetAgeByBirthdate(DateTime yearOfBirth) { DateTime now = DateTime.Now; int age = now.Year - yearOfBirth.Year; if (now.Month < yearOfBirth.Month || (now.Month == yearOfBirth.Month && now.Day < yearOfBirth.Day)) { age--; } return age < 0 ? 0 : age; } } }
这是一部分,但不对,不知道怎么改
qq_45044565 2019-11-24
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("一只" + name + "的" + gender + "正在以" + yearOfBirth + "Km/h的速度在奔跑\n"); Console.ReadLine(); } public class Person { private string name; private DateTime yearOfBirth; private string gender; public Person(string name, DateTime birthday, string gender) { this.name = name; this.yearOfBirth = yearOfBirth; this.gender = gender; } public string Myname { get { return this.name; } set { this.name = value; } } public string Age { get { return this.name; }//只读 } public string MyGender { get { return this.gender; } set { this.gender = value; } } } public int GetAgeByBirthdate(DateTime yearOfBirth) { DateTime now = DateTime.Now; int age = now.Year - yearOfBirth.Year; if (now.Month < yearOfBirth.Month || (now.Month == yearOfBirth.Month && now.Day < yearOfBirth.Day)) { age--; } return age < 0 ? 0 : age; } } }

33,320

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧