private
使用private字段设置变量的访问权限并保护变量不会被随意访问修改
private double Long, Wide, High;
如果需要访问它,则需要通过设置变量的类里设置一个方法来改变它。
public void Set(double Long, double Wide, double High)
{
this.Long = Long;
this.Wide = Wide;
this.High = High;
}//因为三个变量被限制了权限访问,,所以得通过方法来更改
在通过方法访问时,如果在方法内访问了同名变量,可以使用this.来访问自己类里定义的变量
namespace learn.类的调用
{
class Volu
{
private double Long, Wide, High;
//private设置变量访问权限
public void Set(double Long, double Wide, double High)
{
this.Long = Long;
this.Wide = Wide;
this.High = High;
}//因为三个变量被限制了权限访问,,所以得通过方法来更改
public double VolumeNum()
{
return Long * Wide * High;
}
}
}
构造函数
构造函数就是用于初始化数据的函数
声明基本的构造函数的语法就是声明一个和所在类同名的方法,但是该方法没有返回类型。
public class Myclass{
public Myclass(){
这个构造函数的本体
}
}
当我们使用new关键字创建类的时候,就会调用构造方法
构造函数可以重载
using System;
namespace learn.类的调用
{
class Info
{
static void Main(string[] args)
{
double a = 0, b = 0, c = 0;
while (true)
{
try
{
a = Convert.ToDouble(Console.ReadLine());
b = Convert.ToDouble(Console.ReadLine());
c = Convert.ToDouble(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("请输入整数或小数");
}
}
Volu num = new Volu(a, b, c);//使用Volu模板创建一个num的对象并使用构造函数初始化
Console.WriteLine(num.VolumeNum());
//对象的属性以及函数调用
}
}
}
namespace learn.类的调用
{
class Volu
{
public Volu()
{
System.Console.WriteLine("无参数的构造函数");
}
public Volu(double Long, double Wide, double High)
{
this.Long = Long;
this.High = High;
this.Wide = Wide;
System.Console.WriteLine("构造函数对数据初始化");
}
private double Long, Wide, High;
//private设置变量访问权限
public void Set(double Long, double Wide, double High)
{
this.Long = Long;
this.Wide = Wide;
this.High = High;
}//因为三个变量被限制了权限访问,,所以得通过方法来更改
public double VolumeNum()
{
return Long * Wide * High;
}
}
}
属性的定义
定义属性需要名字和类型
使用value关键字代替set中传递的数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn.属性的定义
{
struct Per
{
public string Name;
public int Age;
public string Sex;
}
class Person
{
public Person(string Name, int Age, string Sex)
{
this.My.Name = Name;
this.My.Age = Age;
this.My.Sex = Sex;
}
Per My;
public int Age
{
set
{
this.My.Age = value;
}
get
{
return My.Age;
}
}
}
}
using System;
using learn.属性的定义;
Person my = new Person("TOUming", 11, "boy");
while (true)
{
Console.WriteLine("一年过去了");
my.Age++;
Console.WriteLine("你的岁数" + my.Age);
if (my.Age == 18)
{
Console.WriteLine("你成年了");
break;
}
}
匿名类型
var
当我们创建变量(对象的时候),必须指定类型,但其实也可以不指定类型,这就是匿名类型。
使用var声明的匿名类型,当初始化的时候,这个变量的类型就被确定下来,并且以后不可以更改
堆和栈:程序运行时的内存区域
我们把内存分为堆空间和栈空间
栈空间比较小,但是读取速度块
堆空间比较大,但是读取速度慢
栈
数据只能从栈的顶端插入和删除
堆
堆时一块内存区域,与栈不同,堆里的内存能够以任意顺序存入和移除。
GC Garbage Conllector垃圾回收器
值类型和引用类型
值类型:整数、bool、struct、char、小数
引用类型:string、数组、自定义的类、内置的类
值类型只需要一段单独的内存,用于存储实际的数据:栈
引用类型需要两段内存
第一段存储实际的数据,位于堆
第二段是一个引用,指向数据在堆中的存放位置。
继承
实现继承、接口继承
多重继承
C#不支持多重实现继承,允许类型派生自多个接口:多重接口继承
实现继承
Base.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn.继承
{
class Base
{
private double hp;
private double speed;
private double attack;
//ctrl+r ctrl+e快速生成get set
public double Hp { get => hp; set => hp = value; }
public double Speed { get => speed; set => speed = value; }
public double Attack { get => attack; set => attack = value; }
//public double HP
//{
// get{ return hp; }
// set{ this.hp = value; }
//}
//public double Speed
//{
// set { this.speed = value; }
// get { return this.speed; }
//}
}
}
Boss.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn.继承
{
class Boss : Base
{
public void Attacking()
{
Console.WriteLine("Boss攻击模块正在被调用");
}
}
}
Lenemy.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn.继承
{
class Lenemy : Base
{
public void Attacking()
{
Console.WriteLine("普通敌人的攻击模块正在被调用");
}
}
}
info.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn.继承
{
class info
{
static void Main(string[] args)
{
Lenemy enemy1 = new Lenemy();
enemy1.Hp = 100;
enemy1.Speed = 50;
enemy1.Attack = 20;
Boss boss1 = new Boss();
boss1.Hp = 1000;
boss1.Speed = 20;
boss1.Attack = 50;
enemy1.Attacking();
Console.WriteLine("一位血量{0},速度{1},攻击力{2}的敌人向你发起了攻击", enemy1.Hp, enemy1.Speed, enemy1.Attack);
boss1.Attacking();
Console.WriteLine("一位血量{0},速度{1},攻击力{2}的敌人向你发起了攻击", boss1.Hp, boss1.Speed, boss1.Attack);
}
}
}