博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#以对象为成员的例子
阅读量:3964 次
发布时间:2019-05-24

本文共 2476 字,大约阅读时间需要 8 分钟。

using System;using System.Collections.Generic;using System.Text;namespace test{    class Program    {        static void Main(string[] args)        {            Date birthday = new Date(1999, 11, 11, new Time(16, 33, 22));//传入的第四个参数是对象            Console.WriteLine("我出生于{0}年{1}月{2}日{3}", birthday.year, birthday.month, birthday.day, birthday.clock.To24());//调用第四个对象的方法        }    }    class Time    {        private int hour;        private int minute;        private int second;        private void SetTime(int h, int m, int s)        {            Hour = h;//属性赋值            Minute = m;//属性赋值            Second = s;//属性赋值        }        public Time()//无参构造函数        {            SetTime(0, 0, 0);        }        public Time(int hourvalue)//一参构造函数        {            SetTime(hourvalue, 0, 0);        }        public Time(int hourvalue, int minutevalue, int secondvalue)//三参构造函数        {            SetTime(hourvalue, minutevalue, secondvalue);        }        public int Hour//属性赋值        {            set { hour = (value >= 0 && value <= 24 ? value : 0); }            get { return hour; }        }        public int Minute//属性赋值        {            set { minute = (value >= 0 && value <= 60 ? value : 0); }            get { return minute; }        }        public int Second//属性赋值        {            set { second = (value >= 0 && value <= 60 ? value : 0); }            get { return second; }        }        public string To24()//显示24小时制方法        {            string output = Hour + ":" + Minute + ":" + Second;            return output;        }        public string To12()//显示24小时制方法        {            string output;            if (Hour >= 12)            {                output = Hour % 12 + ":" + Minute + ":" + Second + "PM";            }            else            {                output = Hour % 12 + ":" + Minute + ":" + Second + "AM";            }            /*下面也是可以的            int HOURTEMP = (Hour == 0 || Hour == 12) ? 00 : (Hour % 12);            string PMAM = (Hour < 12) ? "AM" : "PM";            string output1 = HOURTEMP + ":" + Minute + ":" + Second + PMAM;*/            return output;        }    }    class Date    {        public int year;        public int month;        public int day;        public Time clock;//对象定义为成员        public Date(int yearvalue, int monthvalue, int dayvalue, Time clockvalue)        {            year = yearvalue;            month = monthvalue;            day = dayvalue;            clock = clockvalue;        }    }}

 

转载地址:http://pakki.baihongyu.com/

你可能感兴趣的文章
Tomcat在Windows下的免安装配置
查看>>
JMeter常用测试元件
查看>>
JMeter——使用技巧
查看>>
Hibernate 实体层设计--Table per subclass
查看>>
Ruby解决方案:The 'ffi' native gem requires installed build tools ; 含最新DevKit下载地址
查看>>
Python之操作MySQL数据库(二)
查看>>
简单介绍如何使用robotium进行自动化测试
查看>>
Python之操作XML文件
查看>>
eclipse+ADT 进行android应用签名详解
查看>>
Robotium只有apk文件例如Music.apk
查看>>
UI自动化测试框架对比(二)
查看>>
Selenium-webdriver系列教程(9)——如何操作select下拉框
查看>>
Selenium-webdriver系列教程(10)——如何智能的等待页面加载完成
查看>>
Robotium测试NotePad(一)
查看>>
Robotium测试NotePad(二) //测试添加文本
查看>>
Kafka 只有一个broker的例子
查看>>
ZooKeeper 精萃
查看>>
ZooKeeper 简介
查看>>
ZooKeeper 下载和安装
查看>>
只有一个 ZooKeeper 服务器的例子
查看>>