C#面向对象基础

时间:2022-05-01 22:21:32

命名空间/程序集:

命名空间是:namespace 后面可以起名字。在上面的是引用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

用到那个功能就要引用那个using.

如果建立一个新文件夹aaa,并在文件夹中建立一个类,那么要向using中引用才能实现。

访问修饰符:
public - 公共的
private - 私有的

internal - 默认的
protected - 被保护的

类:

成员变量

private string _name;

属性

public string name{

get {return _name;}

set{_name = value;}

}

public decimal scoresum():

带着括号的是方法

比如求和的时候

public decimal scoresum(){

return _cscore+_mscore+_escore;

}

构造函数是默认在每个类中都有一个构造函数,构造函数中没有返回类型,类名叫什么,构造函数叫什么:

默认隐藏。

public  student(){

}

实例化就是执行构造函数的一个过程。

其中静态表示为:

public static decimal jiafa (decimal a , decimal b){

return a+b

}

静态类不需要实例化,直接通过类名点出来:

decimal aaa =100;

decimal bbb=60;

decimal ccc = mamath .jiafa(aaa,bbb);

console.writeLine(ccc);

三局两胜猜拳游戏:

namespace 三局两胜猜拳游戏
{
class Program
{
static void Main(string[] args)
{

Player user = new Player();

Console.Write("请输入您的名称:");
user.Name = Console.ReadLine();

string[] comnames = new string[] { "劫", "盲僧", "小鱼人", "亚索" };
Player com = new Player();

Random r = new Random();
int comind = r.Next(0, comnames.Length);
com.Name = comnames[comind];
Console.WriteLine("==============比赛者入场================");
Console.WriteLine("一号选手:" + user.Name);
Console.WriteLine("二号选手:" + com.Name);
while (true)
{
Console.Write("请输入您的手势:");
user.Hand = Convert.ToInt32(Console.ReadLine());
com.Hand = r.Next(0, 3);

if (user.Hand - com.Hand == -1 || user.Hand - com.Hand == 2) {
Console.WriteLine(user.Name + "手势为:"+user .HandStr );
Console.WriteLine(com.Name + "手势为:"+com.HandStr );
Console.WriteLine("*******" + user.Name + "胜利一局********");
user.Vic += 1;
Console.WriteLine(user.Name + "共胜利"+user.Vic +"局");
Console.WriteLine(com.Name + "共胜利"+com .Vic +"局");


}
else if (user.Hand - com.Hand == -2 || user.Hand - com.Hand == 1)
{

Console.WriteLine(user.Name + "手势为:" + user.HandStr);
Console.WriteLine(com.Name + "手势为:" + com.HandStr);
Console.WriteLine("*******" + com.Name + "胜利一局********");
com.Vic += 1;
Console.WriteLine(user.Name + "共胜利" + user.Vic + "局");
Console.WriteLine(com.Name + "共胜利" + com.Vic + "局");
}
else {
Console.WriteLine(user.Name + "手势为:" + user.HandStr);
Console.WriteLine(com.Name + "手势为:" + com.HandStr);
Console.WriteLine("*******平 局********");
Console.WriteLine(user.Name + "共胜利" + user.Vic + "局");
Console.WriteLine(com.Name + "共胜利" + com.Vic + "局");

}
if (user.Vic == 2) {
Console .WriteLine ("**************************");
Console.WriteLine(user.Name + "获得最终胜利");
Console.WriteLine("***************************");
}
if (com.Vic == 2)
{
Console.WriteLine("**************************");
Console.WriteLine(com.Name + "获得最终胜利");
Console.WriteLine("***************************");
}
Console.ReadLine();
}
}


}
}

namespace 三局两胜猜拳游戏
{
public class Player
{
public Player() {
_Vic = 0;
}
private string _Name;

public string Name
{
get { return _Name; }
set { _Name = value; }
}

private int _Hand;

public int Hand
{
get { return _Hand; }
set { _Hand = value; }
}

private int _Vic;

public int Vic
{
get { return _Vic; }
set { _Vic = value; }
}
public string HandStr{
get {
string s = "石头";
if (_Hand == 1) {
s = "剪刀";
}
else if (_Hand == 2) {
s = "包袱";
}
return s;
}
}

}
}