实验一: 图书类
使用有参与无参构造方法创建对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Book
{
private string bName;
public string Name
{
get { return bName; }
set { bName = value; }
}
private string bAuthor;
public string Author
{
get { return bAuthor; }
set { bAuthor = value; }
}
private double bPrice;
public double Price
{
get { return bPrice; }
set { bPrice = value; }
}
// 无参构造函数
public Book()
{
}
// 有参构造函数
public Book(string bName, string bAuthor, double bPrice)
{
this.bName = bName;
this.bAuthor = bAuthor;
this.bPrice = bPrice;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始");
// 方法一
Book book1 = new Book();
book1.Name = "i love C#";
book1.Author = "李华";
book1.Price = 100;
// 方法二
Book book2 = new Book("i love C++", "Amier", 200);
// 输出结果
Console.WriteLine("用无参构造方法创建的书:");
Console.WriteLine("书名:" + book1.Name + " 作者:" + book1.Author + " 价格:" + book1.Price);
Console.WriteLine();
Console.WriteLine("用有参构造方法创建的书:");
Console.WriteLine("书名:" + book2.Name + " 作者:" + book2.Author + " 价格:" + book2.Price);
}
}
}
运行截图

实验二:图形类
计算矩形和圆的周长与面积
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
public interface IShape
{
// 面积
double Area();
// 周长
double Perimeter();
}
class Rect : IShape
{
// 边长
double width;
// 宽
double height;
// 构造方法
public Rect(double width, double height)
{
this.width = width;
this.height = height;
}
// 实现求面积方法
public double Area()
{
return width * height;
}
// 实现求周长方法
public double Perimeter()
{
return (width + height) * 2;
}
}
class Cricle:IShape
{
// 半径
double r;
// 构造方法
public Cricle(double r)
{
this.r = r;
}
// 实现求面积方法
public double Area()
{
return r*r*3.14;
}
// 实现求周长方法
public double Perimeter()
{
return 2*r*3.14;
}
}
class Program
{
static void Main(string[] args)
{
Rect rect1 = new Rect(5, 3);
Cricle cricle1 = new Cricle(5);
Console.WriteLine("矩形的面积为:"+rect1.Area());
Console.WriteLine("矩形的周长为:" + rect1.Perimeter());
Console.WriteLine("圆的面积为:" + cricle1.Area());
Console.WriteLine("圆的周长为:" + cricle1.Perimeter());
}
}
}
运行截图:

学生类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 学生成绩
{
class Student
{
double chineseScore;
double mathScore;
double englichScore;
private int SID;
public int ID
{
get { return SID; }
set { SID = value; }
}
private string sName;
// 构造函数
public Student(int SID,string sName,double chineseScore, double mathScore, double englichScore)
{
this.chineseScore = chineseScore ;
this.mathScore = mathScore ;
this.englichScore = englichScore ;
this.SID = SID ;
this.sName = sName ;
}
public Student()
{
}
// 信息输入
public static Student inputInfo()
{
Console.WriteLine("请输入学生学号(8位数字):");
string id = Console.ReadLine();
int sid = int.Parse(id);
while(sid > 99999999 || sid < 10000000){
Console.WriteLine("输入格式错误!请重新输入:");
id = Console.ReadLine();
sid = int.Parse(id);
}
Console.WriteLine("请输入学生姓名:");
string name = Console.ReadLine();
for (int i = 0; i < name.Length; )
{
char n_temp = name[i];
// 如果不是字母,则提示重新输入
i++;
if (!char.IsLetter(n_temp))
{
Console.WriteLine("输入学生姓名有误!请重新输入:");
name = Console.ReadLine();
i = 0;
}
}
Console.WriteLine("请输入语文成绩:");
string cs = Console.ReadLine();
double scs = double.Parse(cs);
Console.WriteLine("请输入数学成绩:");
string ms = Console.ReadLine();
double sms = double.Parse(ms);
Console.WriteLine("请输入英语成绩:");
string es = Console.ReadLine();
double ses = double.Parse(es);
Student s = new Student(sid, name, scs, sms, ses);
return s;
}
// 信息输出
public void infoOutput(Student s)
{
Console.WriteLine("学生学号:" + s.ID);
Console.WriteLine("学生姓名:" + s.sName);
Console.WriteLine("语文成绩:" + s.chineseScore);
Console.WriteLine("数学成绩:" + s.mathScore);
Console.WriteLine("英语成绩:" + s.englichScore);
}
// 计算总分
public void sum(Student s)
{
double sum = s.chineseScore + s.mathScore + s.englichScore;
Console.WriteLine("总成绩为:" + sum);
}
}
class Program
{
static void Main(string[] args)
{
Student s = Student.inputInfo();
s.infoOutput(s);
s.sum(s);
}
}
}
运行截图


734

被折叠的 条评论
为什么被折叠?



