using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CircleArea
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.1415926;
double Radius,Area;
Console.Write("请输入要计算面积的圆形的半径:");
Radius = double.Parse(Console.ReadLine());
//Area = PI * Radius * Radius;
Area = PI * Math.Pow(Radius, 2);
Console.WriteLine("半径为{0}的圆形面积为{1}",Radius,Area);
}
}
}
<多态和继承><调用园~计算圆柱体>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ploymorphism
{
class Circle
{
protected double radius;
public Circle(double bj)
{
radius = bj;
}
public double CircleArea()
{
return Math.PI * radius * radius;
}
}
class Cylinder : Circle
{
double high;
public Cylinder(double r, double h)
: base(r)
{
high = h;
}
public double CylinderArea()
{
return base.CircleArea() * 2 + 2 * Math.PI * radius * high;
}
}
class Program
{
static void Main(string[] args)
{
Circle circle = new Circle(100);
Console.WriteLine("半径为100的圆的面积为{0:N2}", circle.CircleArea());
Cylinder cylinder = new Cylinder(100, 100);
Console.WriteLine("半径为100,高为100的圆柱体的表面积为{0:N2}", cylinder.CylinderArea());
}
}
}
<抽象类><抽象属性><抽象方法>
using System;
namespace Abstarct
{
abstract class Person
{
protected int idnumber;
protected string idname;
public abstract int Number {
get;
set;
}
public abstract string Name
{
get;
set;
}
abstract public void Display();
}
class Student : Person
{
private int stunum;
private string stuname;
private string stuclass;
private int score;
public override int Number
{
get
{
return base.idnumber;
}
set
{
base.idnumber = value;
}
}
public override string Name
{
get
{
return base.idname;
}
set
{
base.idname = value;
}
}
public override void Display()
{
Console.WriteLine("Student name:{0}\tnumber:{1}\tclass:{2}\tscore:{3}\tidentity number:{4}\tidentity name:{5}", stuname, stunum, stuclass, score,this.Number,this.Name);
}
public void SetInfo()
{
Console.WriteLine("please input the information of student:");
Console.WriteLine("id number:");
this.Number = int.Parse(Console.ReadLine());
Console.WriteLine("name:");
this.Name = stuname = Console.ReadLine();
Console.WriteLine("student number:");
stunum = int.Parse(Console.ReadLine());
Console.WriteLine("score:");
score = int.Parse(Console.ReadLine());
Console.WriteLine("class:");
stuclass = Console.ReadLine();
}
}
class Teacher : Person
{
private int teanum;
private string teaname;
private string professional;
private string section;
public override int Number
{
get
{
return base.idnumber;
}
set
{
base.idnumber = value;
}
}
public override string Name
{
get
{
return base.idname;
}
set
{
base.idname = value;
}
}
public override void Display()
{
Console.WriteLine("Teacher name:{0}\tnumber:{1}\tclass:{2}\tprofessional:{3}\tidentity:{4}\tidentityname:{5}", teaname, teanum, section,professional,this.Number, this.Name);
}
public void SetInfo()
{
Console.WriteLine("please input the information of teacher:");
Console.WriteLine("id number:");
this.Number = int.Parse(Console.ReadLine());
Console.WriteLine("name:");
teaname = this.Name = Console.ReadLine();
Console.WriteLine("teacher number:");
teanum = int.Parse(Console.ReadLine());
Console.WriteLine("professional:");
professional = Console.ReadLine();
Console.WriteLine("section:");
section = Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
Teacher teacher = new Teacher();
stu.SetInfo();
teacher.SetInfo();
stu.Display();
teacher.Display();
Console.ReadKey();
}
}
}
<调用基类进行计算><嵌套继承>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VirtualFunction
{
class CommonEmployee
{
protected double salary;
protected int workyear;
public virtual double CalSalary() {
return 1000 + workyear * 30;
}
public CommonEmployee(int workyear)
{
this.workyear = workyear;
}
}
class GraduateEmployee : CommonEmployee {
public override double CalSalary()
{
return 2*base.CalSalary();
}
public GraduateEmployee(int workyear) : base(workyear) { }
}
class MasterEmployee:GraduateEmployee {
public override double CalSalary() {
return 1.5 * base.CalSalary();
}
public MasterEmployee(int workyear) : base(workyear) { }
}
class Program
{
static void Main(string[] args)
{
int workyear;
Console.WriteLine("please input the workyear of common employee:");
workyear = int.Parse(Console.ReadLine());
CommonEmployee cemployee;
cemployee = new CommonEmployee(workyear);
Console.WriteLine("please input the workyear of Graduate employee:");
workyear = int.Parse(Console.ReadLine());
GraduateEmployee gemployee;
gemployee = new GraduateEmployee(workyear);
Console.WriteLine("please input the workyear of master employee:");
workyear = int.Parse(Console.ReadLine());
MasterEmployee memployee;
memployee = new MasterEmployee(workyear);
double salary;
salary = cemployee.CalSalary();
Console.WriteLine("普通员工工资:{0}", salary);
salary = gemployee.CalSalary();
Console.WriteLine("本科生工资:{0}", salary);
salary = memployee.CalSalary();
Console.WriteLine("研究生工资:{0}", salary);
Console.ReadLine();
}
}
}
<方法重载>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReloadFunction
{
class Employee
{
private string number;
private string name;
private string professionalTitle;
//构造函数(包含三个参数用于初始化)
public Employee(string enumber,string ename,string etitile)
{
number = enumber;
name = ename;
professionalTitle = etitile;
}
public void getSalary(int bases)// 用于给“研究员”设置工资
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}", number, name, professionalTitle,bases);
}
public void getSalary(int bases,int house)//用于给工程师设置工资(方法名相同,但参数个数不同,方法重载)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3},住房津贴{4}", number, name, professionalTitle,bases,house);
}
public void getSalary(int bases,int house,int bonus)//用于给高级工程师设置工资(方法名相同,但参数个数不同,方法重载)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3},住房津贴{4},奖金{5}", number, name, professionalTitle, bases,house,bonus);
}
}
class Program
{
static void Main(string[] args)
{
string enumber;
string ename;
string etitle;
Employee e1, e2, e3;
//给三个员工设置初始值
Console.WriteLine("请输入NO.1员工的编号:");
enumber = Console.ReadLine();
Console.WriteLine("请输入NO.1员工的姓名:");
ename = Console.ReadLine();
Console.WriteLine("请输入NO.1员工的职称:");
etitle = Console.ReadLine();
e1 = new Employee(enumber, ename, etitle);
Console.WriteLine("请输入NO.2员工的编号:");
enumber = Console.ReadLine();
Console.WriteLine("请输入NO.2员工的姓名:");
ename = Console.ReadLine();
Console.WriteLine("请输入NO.2员工的职称:");
etitle = Console.ReadLine();
e2 = new Employee(enumber, ename, etitle);
Console.WriteLine("请输入NO.3员工的编号:");
enumber = Console.ReadLine();
Console.WriteLine("请输入NO.3员工的姓名:");
ename = Console.ReadLine();
Console.WriteLine("请输入NO.3员工的职称:");
etitle = Console.ReadLine();
e3 = new Employee(enumber, ename, etitle);
//分别对每个对象实例调用不同的方法
e1.getSalary(1000);
e2.getSalary(1200,500);
e3.getSalary(2000,1000,2000);
Console.ReadLine();
}
}
}
<类的嵌套(交互)>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InteractionObject
{
class Course
{
private string coursenumber;
private string coursename;
private double coursepoint;
private string teacher;
//使用方法进行初始化
public void SetInfo(string cnumber,string cname,double cpoint,string cteacher)
{
coursenumber = cnumber;
coursename = cname;
coursepoint = cpoint;
teacher = cteacher;
}
//由于字段成员均为私有且用方法进行初始化,属性里面不用写set
public string Coursenumber
{
get
{
return coursenumber;
}
}
public string Coursename
{
get
{
return coursename;
}
}
public double Coursepoint
{
get
{
return coursepoint;
}
}
public string Teacher
{
get
{
return teacher;
}
}
}
class Student
{
private string number;
private string name;
private double point;
private Course scourse;
//没用方法进行初始化,属性里要写set,输出的时候需要调用,属性里要写get
public string Number
{
get
{
return number;
}
set
{
number = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public double Point
{
get
{
return point;
}
set
{
point = value;
}
}
public Course Scourse
{
get
{
return scourse;
}
set
{
scourse = value;
}
}
//增加学分的方法,利用属性调用了私有的字段成员
public void UpdateCredict()
{
point += scourse.Coursepoint;
}
}
class Program
{
static void Main(string[] args)
{
string Coursename, Classnumber, teacher;
double Coursepoint;
Student s1;
Course c1;
c1 = new Course();
s1 = new Student();
//对课程和学生进行初始化
Console.WriteLine("please input the name of student:");
s1.Name = Console.ReadLine();
Console.WriteLine("please input the number of student:");
s1.Number = Console.ReadLine();
Console.WriteLine("please input the point of student:");
s1.Point = double.Parse(Console.ReadLine());
Console.WriteLine("please input the name of course:");
Coursename = Console.ReadLine();
Console.WriteLine("please input the number of course:");
Classnumber = Console.ReadLine();
Console.WriteLine("please input the teacher of course:");
teacher = Console.ReadLine();
Console.WriteLine("please input the course points of course:");
Coursepoint = double.Parse(Console.ReadLine());
c1.SetInfo(Classnumber, Coursename, Coursepoint, teacher);
s1.Scourse = c1;//用属性进行初始化
s1.UpdateCredict();
Console.WriteLine("student:{0}\tstudent number:{1}\tclass name:{2}\tclass number{3}\tclass teacher:{4}\ttotal point:{5}", s1.Name, s1.Number, s1.Scourse.Coursename, s1.Scourse.Coursepoint, s1.Scourse.Teacher, s1.Point);
Console.ReadLine();
}
}
}
<引用类型作为参数>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculateDistance
{
class Tpoint
{
private double xposition;//两个字段成员分别表示x、y坐标
private double yposition;
public double Xposition//由于是私有的字段成员,除了用方法外,需要使用属性进行赋值
{
set
{
xposition = value;
}
}
public double Yposition
{
set
{
yposition = value;
}
}
public double Odistance(Tpoint p)//欧式距离的方法(由于调用该方法时已经使用一个对象实例,所以传参只需要传入另一个点即可)
{
double distance;
distance = Math.Pow(p.xposition - xposition, 2) + Math.Pow(p.yposition - yposition, 2);
distance = Math.Sqrt(distance);
Console.WriteLine("欧式距离:{0}", distance);
return distance;
}
public double ManHadistance(Tpoint p)//曼哈顿距离的方法
{
double distance;
distance = Math.Abs(p.xposition - xposition) + Math.Abs(p.yposition - yposition);
Console.WriteLine("曼哈顿距离:{0}", distance);
return distance;
}
public double Cosdistance(Tpoint p)//夹角余弦的方法
{
double cos;
cos = ((p.xposition * xposition) + (p.yposition * yposition)) / (Math.Sqrt(Math.Pow(xposition, 2) + Math.Pow(yposition, 2)) * Math.Sqrt (Math.Pow(p.xposition, 2) + Math.Pow(p.yposition, 2)));
Console.WriteLine("余弦距离:{0}", cos);
return cos;
}
}
class Program
{
static void Main(string[] args)
{
Tpoint p1, p2;
p1 = new Tpoint();
p2 = new Tpoint();
Console.WriteLine("please input the X of first point:");
p1.Xposition = double.Parse(Console.ReadLine());
Console.WriteLine("please input the Y of first point:");
p1.Yposition = double.Parse(Console.ReadLine());
Console.WriteLine("please input the X of second point:");
p2.Xposition = double.Parse(Console.ReadLine());
Console.WriteLine("please input the Y of second point:");
p2.Yposition = double.Parse(Console.ReadLine());
p1.Odistance(p2);//对p1调用方法,传入p2
p1.ManHadistance(p2);
p1.Cosdistance(p2);
Console.ReadLine();
}
}
}
<枚举类型>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnSpecial
{
class Program
{
enum Week
{
Sunday,
Monday,
Tuesday,
Wenesday,
Thursday,
Friday,
Saturday
}
static void Main(string[] args)
{
Console.WriteLine("请输入星期:");
Week week = (Week)int.Parse(Console.ReadLine());
switch (week)
{
case Week.Sunday:
Console.WriteLine("星期日特价菜:爆炒牛肉18元");
break;
case Week.Monday:
Console.WriteLine("星期一特价菜:啤酒鸭26元");
break;
case Week.Tuesday:
Console.WriteLine("星期二特价菜:红烧肉20元");
break;
case Week.Wenesday:
Console.WriteLine("星期三特价菜:回锅肉16元");
break;
case Week.Thursday:
Console.WriteLine("星期四特价菜:水煮鱼24元");
break;
case Week.Friday:
Console.WriteLine("星期五特价菜:剁椒鱼头30元");
break;
case Week.Saturday:
Console.WriteLine("星期六特价菜:手撕包菜12元");
break;
default:
break;
}
}
}
}
<结构体>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Struct
{
class Program
{
struct Rectangle
{
public double Width;
public double Height;
public bool IsSquare()
{
return Width == Height;
}
};
static void Main(string[] args)
{
Rectangle rect;
string issquare;
Console.WriteLine("请输入矩形的长:");
rect.Height = double.Parse(Console.ReadLine());
Console.WriteLine("请输入矩形的宽:");
rect.Width = double.Parse(Console.ReadLine());
if (rect.IsSquare())
{
issquare = "是";
}
else
{
issquare = "不是";
}
Console.WriteLine("长为{0},宽为{1}的矩形{2}正方形!", rect.Height, rect.Width, issquare);
}
}
}