1.面向对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01Test
{
class Program
{
static void Main(string[] args)
{
// string s;
// Person sunQuan;//自定义类
// 创建Person类的对象
Person suQuan = new Person();
suQuan.Name = "孙全";
suQuan.Age = -23;
suQuan.Gender = '春';
suQuan.Say();
Console.ReadKey();
}
}
}
Person类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01Test
{
public class Person
{
private string _name;
public string Name
{
//当你输出属性的值得时候 会执行get方法
get { return _name; }
//当你给属性赋值的时候 首先会执行set方法
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 100)
{
value = 0;
}
_age = value;
}
}
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
public void Say()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我可以说话", this.Name, this.Age, this.Gender);
}
}
}
2.静态与非静态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02Test
{
class Program
{
static void Main(string[] args)
{
//调用实例成员
Person p = new Person();
p.M1();//实例方法
Person.M2();//静态方法
// Student s = new Student();
Console.WriteLine();
Console.ReadKey();
}
}
}
Person类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02Test
{
public class Person
{
private static string _name;
public static string Name
{
get { return Person._name; }
set { Person._name = value; }
}
private char _gender;
public char Gender
{
get { return _gender; }
set { _gender = value; }
}
public void M1()
{
Console.WriteLine("我是非静态的方法");
}
public static void M2()
{
Console.WriteLine("我是一个静态方法");
}
}
}
Student类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02Test
{
public static class Student
{
private static string _name;
public static string Name
{
get { return Student._name; }
set { Student._name = value; }
}
public static void M1()
{
Console.WriteLine("Hello World");
}
// private int _age;
}
}
3.练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03Test
{
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三",100,100,100);
Console.ReadKey();
//Student zsStudent = new Student("张三", 18, '男', 100, 100, 100);
//zsStudent.SayHello();
//zsStudent.ShowScore();
//Student xlStudent = new Student("小兰", 16, '女', 50, 50, 50);
//xlStudent.SayHello();
//xlStudent.ShowScore();
//Console.ReadKey();
}
}
}
Student类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03Test
{
public class Student
{
//字段、属性、方法、构造函数
//析构函数 构造函数
//当程序结束的时候 析构函数才执行
//帮助我们释放资源
//GC Garbage Collection
~Student()
{
Console.WriteLine("我是析构函数");
}
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;
}
public Student(string name, int chinese, int math, int english):this(name,0,'c',chinese,math,english)
{
//this.Name = name;
//this.Chinese = chinese;
//this.Math = math;
//this.English = english;
}
public Student(string name, int age, char gender)
{
this.Name = name;
if (age < 0 || age > 100)
{
age = 0;
}
this.Age = age;
this.Gender = gender;
}
public Student()
{
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 100)
{
value = 0;
}
_age = value;
}
}
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _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 ShowScore()
{
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
}
}
}
4.练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04Test
{
class Program
{
static void Main(string[] args)
{
Ticket t = new Ticket(150);
t.ShowTicket();
Console.ReadKey();
}
}
}
Ticket类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04Test
{
public class Ticket
{
//写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),
//不能为负数,有一个价格属性,价格属性只读,
//并且根据距离distance计算价格Price (1元/公里):
// 0-100公里 票价不打折
//101-200公里 总额打9.5折
//201-300公里 总额打9折
//300公里以上 总额打8折
private double _distance;
public double Distance
{
get { return _distance; }
}
public Ticket(double distance)
{
if (distance < 0)
{
distance = 0;
}
this._distance = distance;
}
private double _price;
// 0-100公里 票价不打折
//101-200公里 总额打9.5折
//201-300公里 总额打9折
//300公里以上 总额打8折
public double Price
{
get
{
if (_distance > 0 && _distance <= 100)
{
return _distance * 1.0;
}
else if (_distance >= 101 && _distance < 200)
{
return _distance * 0.95;
}
else if (_distance >= 201 && _distance < 300)
{
return _distance * 0.9;
}
else
{
return _distance * 0.8;
}
}
}
public void ShowTicket()
{
Console.WriteLine("{0}公里需要{1}元",Distance,Price);
}
}
}