1.面向对象简介
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_Sharp面向对象基础
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
//p1.Name="tom";
p1.Height = 180;
p1.Age = 30;
p1.SayHello();
Person2 p2 = new Person2();
p2.Age = 20;
p2.GiveName("tom");
p2.SayHello();
Person3 p3=new Person3();
p3.Age=30;
//Console.WriteLine("年龄是{0}",p3.age);
p3.Age=-1;
Console.WriteLine("年龄是{0}",p3.Age);
p3.Age1=-100;
Console.WriteLine("年龄是{0}",p3.Age1);
Person4 p4 = new Person4();
p4.Age = 30;
p4.Age = p4.Age + 1;
Console.WriteLine(p4.Age);
//Person5 p5 = new Person5();
//p5.Age = 30;
//Console.WriteLine(p5.Age);
Person6 p6 = new Person6();
p6.IncAge();
p6.IncAge();
//p6.Age = 30;
Console.WriteLine("年龄{0}",p6.Age);
Person7 p7 = new Person7();
p7.Age = 30;
Console.WriteLine(p7.Age);
Console.ReadKey();
}
class Person
{
public int Height;
public int Age;
private string Name;
public void SayHello()
{
Console.WriteLine("大家好,我叫{0},我的身高是{1},我的年龄是{2}",this.Name,this.Height,this.Age);
}
}
class Person2
{
public int Height;
public int Age;
private string Name;
public void GiveName(string name)
{
if (name == "jerry")
{
return;
}
this.Name=name;
}
public void SayHello()
{
睁眼();
Console.WriteLine("大家好,我叫{0},我的身高是{1},我的年龄是{2}", this.Name, this.Height, this.Age);
}
private void 睁眼()
{
Console.WriteLine("睁开双眼");
}
}
class Person3
{
private int age;
public int Age1;
public int Age
{
set //赋值
{
if(value<0)
{
return;
}
this.age=value;
}
get //取值
{
return this.age;
}
}
}
class Person4
{
public int Age
{
set
{
}
get
{
return 3;
}
}
}
class Person5
{
private int age;
public int Age
{
set
{
this.Age = value;//给自己赋值,死循环
}
get
{
return this.Age;
}
}
}
class Person6
{
private int age;
public int Age //只读属性,因为只有get,没有set
{
get { return age; }
}
public void IncAge()
{
age++;
}
}
class Person7
{
public int Age
{
get; //编译器自动帮我们生成私有字段和set、get代码块。
set;
}
public string Name
{
get;
set;
}
}
}
}
2..聊天机器人
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 面向对象版聊天机器人
{
class Program
{
static void Main(string[] args)
{
机器人 r1 = new 机器人();
r1.Name = "小I";
r1.Eat(5);
机器人 r2 = new 机器人();
r2.Name = "小J";
r1.Eat(8);
Console.WriteLine();
Console.WriteLine("请选择机器人:1→小I,2→小J");
机器人 r;
string str = Console.ReadLine();
if (str == "1")
{
r = r1; //r指向"r1指向的对象"
}
else
{
r = r2;
}
r.SayHello();
while (true)
{
string str1 = Console.ReadLine();
r1.Speak(str1);
}
Console.ReadKey();
}
class 机器人
{
public string Name { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫:{0}",Name);
}
public void Eat(int foodCount)
{
if (FullLevel > 100)
{
return;
}
FullLevel = FullLevel + foodCount;
}
public void Speak(string str)
{
if (FullLevel <= 0)
{
Console.WriteLine("饿死了,不说了!");
return;
}
if (str.Contains("姓名") || str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!");
}
else
{
Console.WriteLine("听不懂!");
}
FullLevel--;
}
}
}
}
3.构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 构造函数1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
Person p2 = new Person("tom");
Person p3 = new Person("jerry", 20);
Person p4 = new Person();
Console.WriteLine("年龄:{0},姓名:{1}",p1.Age,p1.Name);
Console.WriteLine("年龄:{0},姓名:{1}", p2.Age, p2.Name);
Console.WriteLine("年龄:{0},姓名:{1}", p3.Age, p3.Name);
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person()
{
Name = "未命名";
Age = 0;
}
public Person(string name)
{
this.Name = name;
}
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
}
4.继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 继承
{
class Program
{
static void Main(string[] args)
{
中国人 c1=new 中国人();
c1.Height=180;
c1.Name="李小龙";
c1.SayHello();
c1.户口="北京";
韩国人 k1=new 韩国人();
k1.Name="金喜善";
k1.Height=170;
k1.SayHello();
k1.做泡菜();
Person p1 = c1;
p1.SayHello();
Person p2 = k1;
k1.SayHello();
//中国人 zgr = p1; //报错,因为p1是Person类
中国人 zgr=(中国人)p1;
zgr.SayHello();
//中国人 zgr1 = (中国人)p2; //一旦程序员的保证不靠谱,照样报错
//zgr1.SayHello();
Object obj = 3;
//Object是所有类的一个基类
Console.ReadKey();
}
class Person //所有的类都继承于Object类,等价于Person:Object
{
public string Name{get;set;}
public int Age{get;set;}
public int Height{get;set;}
public void SayHello()
{
Console.WriteLine("SayHello", this.Name);
}
}
class 中国人:Person
{
public string 户口{get;set;}
public void 功夫()
{
Console.WriteLine("我打!!!");
}
}
class 韩国人 : Person
{
public string 饭量 { get; set; }
public void 做泡菜()
{
Console.WriteLine("泡菜香!");
}
}
}
}
5.异常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 异常
{
class Program
{
static void Main(string[] args)
{
/*
try
{
Console.WriteLine("Convert之前");
int i=Convert.ToInt32("abc");
Console.WriteLine("Convert之后");
}
catch(Exception ex)
{
Console.WriteLine("数据错误:"+ex.Message+"。异常堆栈"+ex.StackTrace);
}
Console.WriteLine("ReadKey之前");
Console.ReadKey();
//DeleteFile("c:/1.avi");
//DeleteFile("c:/2.avi");
//int i = Convert.ToInt32("abc");
*/
try
{
string desc = GetAgeDesc(300);
}
catch (Exception ex)
{
Console.WriteLine("数据错误:"+ex.Message);
}
Console.ReadKey();
}
static string GetAgeDesc(int age)
{
if (age >= 0 && age <= 3)
{ return "婴幼儿"; }
else if (age > 3 && age <= 18)
{ return "青少年"; }
else if (age > 19 && age < 150)
{ return "大人"; }
else if (age < 0)
{ throw new Exception("您来自反物质世界吧!"); }
else
{ throw new Exception("您见过老佛爷吧!"); }
}
static int DeleteFile(string filepath)
{
//尝试删除文件,发现无法删除
return -1;
//return 0。如果没有权限 return -2,找不到删除的文件return -3。
}
}
}
6.常量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 常量
{
class Program
{
public const int pi = 3;//不用new一个类就可以直接调
static void Main(string[] args)
{
int r = 10;
//int pi = 3;
//const int pi = 3;//pi声明为常量
int l = 2 * pi* r;
Console.WriteLine("周长:{0}",l);
int m = pi * r * r;
Console.WriteLine("面积:{0}",m);
//Math.PI;
Console.ReadKey();
}
}
}
7.静态成员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 静态成员
{
class Program
{
static void Main(string[] args)
{
Person.TotalCount = 30;
Console.WriteLine(Person.TotalCount);
DoIt();
Dog d = new Dog();
d.叫唤();
Person.人口汇报();
Person p1 = new Person();
p1.Age = 300;
p1.SayHello();
Console.ReadKey();
}
public static void DoIt()
{
Console.WriteLine("ffff");
Console.WriteLine("使用全局变量:{0}",Person.TotalCount);
}
}
class Person
{
public static int TotalCount;//静态成员
public int Age;
public static void 人口汇报() //static函数
{
//Console.WriteLine("总人口:{0},年龄:{1}",TotalCount,Age);
//static函数中不能调用非static成员
}
public void SayHello() //非static函数
{
Console.WriteLine("我的年龄:{0},全球总人口:{1}",Age,TotalCount);
//非static函数可以调用static成员
}
}
class Dog
{
public void 叫唤()
{
Console.WriteLine("旺旺:{0}",Person.TotalCount);
}
}
//静态类
static class ConsoleHelper //静态类不能被实例化,也就是不能被new的类
{
public static int ReadInt()
{
string str=Console.ReadLine();
return Convert.ToInt32(str);
}
}
}
8.命名空间
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 命名空间.hr; //如果要使用的类和当前的类不在同一个namespace,则要加using引用
using 命名空间.oa;
using System.Collections;
namespace 命名空间
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
命名空间.hr.Person p2 = new 命名空间.hr.Person();
//就像文件的全路径一样
Dog d = new Dog();
Mouse m = new Mouse();
ArrayList list = new ArrayList();
//在ArrayList上点右键->解析->using System.Collections
Console.ReadKey();
}
}
class Person
{
}
}
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 命名空间.hr
{
class Person
{
}
}
Dog.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 命名空间.hr
{
class Dog
{
}
}
Mouse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 命名空间.oa
{
class Mouse
{
}
}
9.索引器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 索引
{
class Program
{
static void Main(string[] args)
{
int[] values = { 3,5,9,8};
int i=values[1];
Person p1 = new Person();
p1[1] = "小明";
Console.WriteLine(p1[1]+p1[2]);
string s = "";
//s[0]='aa'; //因为s是string类型,是一个只读的索引器
Console.WriteLine(p1["tom",3,9]);
//p1["tom", 3, 9] = "aaa"; //报错,因为p1只读
Console.ReadKey();
}
}
class Person
{
private string FirstName="大毛";
private string SecondName="二毛";
public string this[string name, int x, int y]//索引的参数可以多个
{
get
{
return name + x + y;
}
}
public string this[int index] //索引器
{
set
{
if (index == 1)
{
FirstName = value;
}
else if (index == 2)
{
SecondName = value;
}
else
{
throw new Exception("错误的序号");
}
}
get
{
if (index == 1)
{
return FirstName;
}
else if (index == 2)
{
return SecondName;
}
else
{
throw new Exception("错误的序号");
}
}
}
}
}