<C#任务导引教程>练习七
//55,类的声明示例
using System;
class Date
{
public int year;
public int month;
public int day;
public void print()
{
Console.WriteLine("{0}/{1:D2}/{2:D2}", year, month, day);
}
}
class Program
{
static void Main()
{
Date t = new Date();
Console.Write("请输入年:");
t.year = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入月:");
t.month = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入年:");
t.day = Convert.ToInt32(Console.ReadLine());
t.print();
}
}
//56,利用“属性”访问私有数据成员示例
using System;
class Date
{
int year;
int month;//默认私有成员
int day;
public int Year
{
get { return year; }
set { year = value; }
}
public int Month
{
get { return month; }
set { month = value; }
}
public int Day
{
get { return day; }
set { day = value; }
}
public string Print//定义日期信息属性
{
get
{
return Year + "/" + Month + "/" + Day;
}
}
}
class Program
{
static void Main()
{
Date d = new Date();
d.Year = 2010;
d.Month = 4;
d.Day = 20;
Console.WriteLine(d.Print);
}
}
//57,利用'属性"和"方法"访问私有数据成员示例
using System;
class Date
{
int year, month, day;
public int Year
{
set { year = value; }
}
public int Month
{
set { month = value; }
}
public int Day
{
set { day = value; }
}
public void Print()
{
Console.WriteLine("year:" + 2010);
Console.WriteLine("month:" + 4);
Console.WriteLine("day:" + 20);
}
}
class Program
{
public static void Main()
{
Date d = new Date();
d.Print();
}
}
//58,声明一个正方形类型和一个圆类型,然后分别求他们的面积
using System;
class Square
{
int length;
public void set(int l)
{
length = l;
}
public int area()
{
return length * length;
}
}
class Circle
{
int length;
public void set(int l)
{
length = l;
}
public double area()
{
return 3.1416 * length * length / 4;
}
}
class Program
{
static void Main()
{
Square s = new Square();
s.set(15);
Console.WriteLine("正方形面积为:{0}", s.area());
Circle c = new Circle();
c.set(10);
Console.WriteLine("圆形面积为:{0}", c.area());
Console.ReadLine();
}
}
//59,声明一个日期类,并进行日期的设置,判断闰年和输出等有关操作
using System;
class Date
{
int year, month, day;
public void set(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
public bool isLeapYear()
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public void print()
{
Console.WriteLine("{0}-{1:D2}-{2:D2}", year, month, day);
}
}
class Program
{
static void Main()
{
Date d = new Date();
d.set(2000, 12, 6);
if (d.isLeapYear())
d.print();
}
}
//60,构造函数的定义
using System;
class Box
{
public Box(int h, int w, int l)
{
height = h;
width = w;
length = l;
}
public int volume()
{
return (height * width * length);
}
private int height, width, length;
}
class Program
{
static void Main()
{
Box box1 = new Box(12, 25, 30);
Console.WriteLine("长方形box1的体积是:{0}", box1.volume());
Box box2 = new Box(15, 30, 21);
Console.WriteLine("长方形box2的体积是:{0}", box2.volume());
}
}
//61,使用不同参数用构造函数对成员变量赋初值示例。重载构造函数
using System;
class Box
{
public Box()
{
height = 10;
width = 10;
length = 10;
}
public Box(int h)
{
height = h;
width = 10;
length = 10;
}
public Box(int h,int w)
{
height = h;
width = w;
length = 10;
}
public Box(int h,int w,int l)
{
height = h;
width = w;
length = l;
}
public int volume()
{
return (height * width * length);
}
private int height,width,length;
}
class Program
{
static void Main()
{
Box box1=new Box();
Console.WriteLine("长方形box1的体积是:{0}",box1.volume());
Box box2=new Box(15);
Console.WriteLine("长方形box2的体积是:{0}",box2.volume());
Box box3=new Box(15,30);
Console.WriteLine("长方形box3的体积是:{0}",box3.volume());
Box box4=new Box(15,30,20);
Console.WriteLine("长方形box4的体积是:{0}",box4.volume());
}
}
//62,析构函数
using System;
class Date
{
public Date(int y, int m, int d)
{
year = y;
month = m;
day = d;
Console.WriteLine("{0}:构造函数已被调用.", day);
}
~Date()
{
Console.WriteLine("{0}:析构函数已被调用", day);
}
public void print()
{
Console.WriteLine("{0}.{1}.{2}", year, month, day);
}
private int year;
private int month;
private int day;
}
class Program
{
static void Main()
{
Date today = new Date(2010, 9, 1);
Date tomorrow = new Date(2010, 9, 2);
Console.Write("今天是:");
today.print();
Console.Write("明天是:");
tomorrow.print( );
}
}
//63,用不同对象调用同一个函数成员输出学生的平均分示例
using System;
class Student
{
float score1;
float score2;
public Student(float s1, float s2)
{
score1 = s1;
score2 = s2;
}
public float ave()
{
float average;
average = (score1 + score2) / 2;
return average;
}
}
class Program
{
static void Main()
{
Student Zhang = new Student(89.0f, 94.5f);
Student Li = new Student(88.0f, 89.5f);
Student Wang = new Student(90.0f, 82.5f);
Console.WriteLine("张的平均成绩是:{0}", Zhang.ave( ));
Console.WriteLine("李的平均成绩是:{0}", Li.ave());
Console.WriteLine("王的平均成绩是:{0}", Wang.ave());
}
}
//64,使用多个对象处理有关学生类问题的示例
using System;
class Student
{
public Student(int n, int a, int s)
{
num=n;
age = a;
score = s;
}
public void print()
{
Console.WriteLine("num:{0}", num);
Console.WriteLine("age:{0}", age);
Console.WriteLine("score:{0}", score);
}
private int num, age, score;
}
class Program
{
static void Main()
{
Student stud1 = new Student(1001, 18, 87);
Student stud2 = new Student(1002, 19, 76);
Student stud3 = new Student(1003, 18, 72);
stud1.print();
stud2.print();
stud3.print();
}
}
//65.使用对象数组来处理学生类问题
using System;
class Student
{
public Student(int n, int a, int s)
{
num = n;
age = a;
score = s;
}
public void print()
{
Console.WriteLine("学号: {0}", num);
Console.WriteLine("年龄: {0}", age);
Console.WriteLine("成绩: {0}", score);
}
private int num, age, score;
}
class Program
{
static void Main( )
{
Student[] stud = new Student[3] { new Student(1001, 18, 87), new Student(1002, 19, 76), new Student(1003, 18, 72) };
for (int i = 0; i < stud.Length; i++)
{
stud[i].print();
}
}
}
<C#任务导引教程>练习七的更多相关文章
- Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题
从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...
- Docker入门教程(七)Docker API
Docker入门教程(七)Docker API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第七篇,重点介绍了Docker Registry API和 ...
- HMM 自学教程(七)前向后向算法
本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...
- Ocelot简易教程(七)之配置文件数据库存储插件源码解析
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9852711.html 上篇文章给大家分享了如何集成我写的一个Ocelot扩展插件把Ocelot的配置存储 ...
- Java NIO系列教程(七) selector原理 Epoll版的Selector
目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...
- iOS 11开发教程(七)编写第一个iOS11代码Hello,World
iOS 11开发教程(七)编写第一个iOS11代码Hello,World 代码就是用来实现某一特定的功能,而用计算机语言编写的命令序列的集合.现在就来通过代码在文本框中实现显示“Hello,World ...
- (转)微信公众平台开发教程(七)Session处理
原文地址:http://www.cnblogs.com/yank/p/3476874.html 微信公众平台开发教程(七)Session处理 在微信窗口,输入的信息有限,我们需要将一些信息分多次请求. ...
- Photoshop入门教程(七):蒙版
学习心得:蒙版在Photoshop中也是很常用的,学会使用蒙版,可以提高图像处理能力,并且可以保护原片不被破坏,建议多使用一些蒙版. 蒙板是灰度的,是将不同灰度色值转化为不同的透明度,并作用到它所在的 ...
- struts2官方 中文教程 系列七:消息资源文件
介绍 在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定).消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字 ...
随机推荐
- TypeScript 枚举指南
枚举是受 TypeScript 支持的数据类型.枚举允许您定义一组命名常量.使用它们可以更轻松地记录意图或创建一组不同的案例.枚举大多数用于面向对象的编程语言(如 Java 和 C#)中,现在也可以 ...
- 洛谷2093 JZPFAR + KD-Tree学习笔记 (KD-Tree)
KD-Tree这玩意还真的是有趣啊.... (基本完全不理解) 只能谈一点自己的对KD-Tree的了解了. 首先这个玩意就是个暴力... 他的结构有点类似二叉搜索树 每一层都是以一个维度作为划分标准. ...
- Flutter随笔(二)——使用Flutter Web + Docker + Nginx打造一个简单的Web项目
前言 Flutter作为一个跨平台UI框架,功能十分强大,仅用一套代码便能编译出Android.iOS.Web.windows.macOS.Windows.Linux等平台上的应用,各平台应用体验高度 ...
- Python 面向对象笔记
Python 面向对象课程笔记 前言 Python 面向对象 正文 基本概念 什么是对象: 万物皆对象 对象是具体物体: 拥有属性 拥有行为 封装零散为整体 OOP(Object Oriented P ...
- iOS路由最佳选择是什么
背景 记得四年前iOS路由开始盛行,当时比较有名的是蘑菇街的,后来CTMediator写了几篇文章把蘑菇街批的体无完肤,导致我后来写新项目用了CTMediator,那一堆组件创建的叫一个酸爽啊!再后来 ...
- for...of 和 for...in 是否可以直接遍历对象,有什么解决方案
答案: for...of不能直接遍历对象,for in可以直接遍历对象 原因: for...of需要实现iterator接口,对象没有实现iterator接口 解决: const obj = {a: ...
- 初始HTML03
**------------恢复内容开始------------** HTML 页面标签组成 一个完整的页面仅有一个html元素,在这个元素之下,包含head和body元素,前者负责说明页面结构,后者 ...
- 6月4日 Scrum Meeting
日期:2021年6月4日 会议主要内容概述:讨论账单功能模块,讨论账单前后端接口. 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中遇到的困难 徐宇龙 后端 账单数据界面 设 ...
- jzoj6094
题目描述 给定一个循环流(每个点均满足流量平衡条件),这个循环流有$n$个点,且每条边的流量只有 $1$ 或$ 2$,其中$a$条边流量为$1$,$b$条边流量为$2$,判断是否存在一个流满足上述条件 ...
- matlab添加永久路径
addpath('D:\MATLAB6p5\toolbox\svm'); 临时添加路径,不能添加子目录 addpath(genpath('D:\MATLAB6p5\toolbox\svm'));临时添 ...