day 14 C#打卡
1.创造一个学生类输出学生信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Student
{
private string name;
public string Name
{
get { return name; }
set
{ name = value; }
}
public int age;
public int Age
{
get { return age; }
set
{
if (value > 0 && value < 100)
age = value;
else age = 18;
}
}
private string hobby;
public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
static void Main(string[] args)
{
Student student = new Student();//类名 对象=new 类名()
Student student2 = new Student();
Student student3 = new Student();
student.Name = "张三";//对象.方法名=值
student.Age = 18;
student.Hobby = "踢球";
student2.Name = "李四";
student2.Age = 19;
student2.Hobby = "绘画";
student3.Name = "王五";
student3.Age = 19;
student3.Hobby = "烫头";
string message = string.Format("我叫{0},今年{1}岁,喜欢{2}。\n我叫{3},今年{4}岁,喜欢{5}。\n我叫{6},今年{7}岁,喜欢{8}。", student.Name, student.Age, student.Hobby,
student2.Name, student2.Age, student2.Hobby, student3.Name, student3.Age, student3.Hobby);
Console.WriteLine(message);
}
}
}
2.调用方法输出学生信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Student
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
}
}
public int age;
public int Age
{
get { return age; }
set
{
if (value > 0 && value < 100)
age = value;
else age = 18;
}
}
private string hobby;
public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
public void setvalue(string Name, int Age, string Hobby)
{
name = Name; age = Age; hobby = Hobby;
}
public void putvalue()
{
Console.WriteLine("我叫{0},今年{1}岁,喜欢{2}", name, age, hobby);
}
static void Main(string[] args)
{
Student student = new Student();
student.setvalue("张三", 18, "踢球");
student.putvalue();
Student student2 = new Student();
student2.setvalue("李四", 19, "运动");
student2.putvalue();
Student student3 = new Student();
student3.setvalue("王五", 20, "游泳");
student3.putvalue();
}
}
}
3.运用属性限制字段
using System;
namespace MySchool
{
public class Student //定义一个类
{
public string _name;//字段
private string Name //属性
{
get { return _name; }//返回_name的值输出
set { _name = value; }//从主函数那里获取_name的值
}
public int _age;
public int Age //属性,就是对字段进行限制,
//值不存在属性里,而是通过属性这个中间商
//将值赋给字段。
{
get
{
if (_age > 120 || _age <= 0)
_age = 18;
return _age;
}
set { _age = value; }
}
public string _gender;
private string Gender
{
get
{
if (_gender != "男" && _gender != "女")
return _gender = "女";
return _gender;
}
set { _gender = value; } //程序运行时,先执行set方法赋值,再执行get方法返回值
}
public void behavior()
{
Console.WriteLine("我的名字是{0},年龄{1},是一个{2}生", this.Name, this.Age, this.Gender);
//打印的时候应该是输出属性,不然输出字段,属性的限制则没有起作用
}
}
class program
{
static void Main(string[] args)
{
Student student = new Student(); //对类进行实例化,student也就是对象
student._name = "美丽";
student._age = -23;
student._gender = "春";
student.behavior();
}
}
}
4.构造函数的运用(1)
using System;
namespace CAR
{
class Car
{
public Car(string company, string brand, int total, int sell)//构造函数
{
int @int = total - sell;
Console.WriteLine(company + brand + "总量为:" + total + ",即将卖出:" +
sell + "辆 ,剩余" + @int + "辆");
}
~Car()
{
Console.WriteLine("信息已处理完毕!");
}
static void Main(string[] args)
{
Car Benz = new Car("某某公司", "奔驰", 20000, 15000);
}
}
}
构造函数的运用(2)
using System;
namespace MySchool
{
class Student
{
string strname;
public Student(string name)//一个参数的构造函数
{
strname = name;
Console.WriteLine("姓名:" + name + " 性别:未知 年龄:未知");
}
public Student(string name, string sex)//两个参数的构造函数
{
strname = name;
Console.WriteLine("姓名:" + name + " 性别:" + sex + " 年龄:未知");
}
public Student(string name, string sex, int age)//三个参数的构造函数
{
strname = name;
Console.WriteLine("姓名:" + name + " 性别:" + sex + " 年龄:" + age);
}
~Student()//析构函数
{
Console.WriteLine("学生" + strname + "信息输出完毕!");
}
static void Main(string[] args)
{
Console.WriteLine("输出学生信息:");
Student stu1 = new Student("小张");//实例化类的实例
Student stu2 = new Student("小王", "男");//有n个值则对应有n个参数的构造函数里
Student stu3 = new Student("小冯", "女", 22);
}
}
}
构造函数的运用(3)
using System;
namespace Program1
{
class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public int age;
public int Age
{
get { return age; }
set
{
if (value < 0 || value > 100) value = 18;
//在set方法里面对字段进行限制
age = value;
}
}
private char gender;
public char Gender
{
get { return gender; }
set
{
if (gender != '男' && gender != '女')
gender = '女';
gender = value;
}
}
private int chinese;
public int Chinese
{
get { return chinese; }
set { chinese = value; }
}
private int math;
public int Math
{
get { return math; }
set { math = value; }
}
private int english;
public int English
{
get { return english; }
set { english = value; }
}
public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
}
public void Score()
{
Console.WriteLine("我的总成绩是{0},平均分是{1}", this.Chinese + this.Math + this.English,
(this.Chinese + this.Math + this.English) / 3);
}
public Student(string name,int age,char gender,int chinese,int math,int english)//创建构造函数
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}
static void Main(string[] args)
{
Student zsstudent = new Student("张三", 18, '男', 98, 78, 100);
zsstudent.SayHello();
zsstudent.Score();
Student lsstudent = new Student("李四", 19, '男', 90, 88, 99);
lsstudent.SayHello();
lsstudent.Score();
}
}
}
构造函数的作用:帮助我们初始化对象,给对象的每个属性依次赋值。
构造函数没有返回值,连void也不能写;构造函数的名称必须跟类名一样。
创建对象的时候执行构造函数。
以上程序没有运行到析构函数的原因是:程序员无法控制何时调用析构函数,因为这是由垃圾回收器决定的。
5.使用字段、属性、方法、构造函数,输入三角形的三边,输出三角形的周长和面积
using System;
namespace Triangle1
{
class Triangle
{
private double a;
private double b;
private double c;
public double A
{
get { return a; }
set { a = value; }
}
public double B
{
get { return b; }
set { b = value; }
}
public double C
{
get { return c; }
set { c = value; }
}
public Triangle(double aa,double bb,double cc)//构造函数赋初始值
{
a = aa;b = bb;c = cc;
Console.WriteLine("三角形的三边分别是{0}、{1}、{2}", aa, bb, cc);
}
public void Area()//求面积的方法
{
double d = (this.A + this.B + this.C) / 2;
double area = Math.Sqrt(d * (d - this.A) * (d - this.B) * (d - this.C));
Console.WriteLine("三角形的面积是:{0}",area);
}
public void Perimeter()//求周长的方法
{
double perimeter = this.A + this.B + this.C;
Console.WriteLine("三角形的周长是:{0}", perimeter);
}
static void Main(string[] args)
{
label: Console.WriteLine("请输入三角形的三边(用回车隔开):");
string aa = Console.ReadLine();
string bb = Console.ReadLine();
string cc = Console.ReadLine();
if (double.Parse(aa) > 0 && double.Parse(bb) > 0 && double.Parse(cc) > 0 && (double.Parse(aa) + double.Parse(bb)
> double.Parse(cc))&& (double.Parse(bb) + double.Parse(cc) > double.Parse(aa)) && (double.Parse(aa)
+ double.Parse(cc) > double.Parse(bb)))
Console.WriteLine("可以构成三角形!");
else
{
Console.WriteLine("不能构成三角形!请再输入一遍!");
goto label;
}
Triangle triangle = new Triangle(double.Parse(aa), double.Parse(bb),double.Parse(cc));
triangle.Area();//调用面积方法输出面积
triangle.Perimeter();//调用周长方法输出周长
}
}
}