struct 结构体
结构体
-
结构体为值类型和类的用法很像
-
结构体中也可以存成员变量和以及成员方法
-
结构体不需要构造方法,因为是值类型存储咋栈中
-
不使用New也是可以使用
-
自定义结构体本身可以继承接口不能继承类,也不能被继承
只是存储数据建议使用结构体,有复杂逻辑建议使用类。
struct
关键词
using System;
namespace Test {
class Program {
static void Main(string[] args) {
Student st = new Student("张三", 188, 70, Sex.female);
// 因为是值类型,故不需要new也是可以赋值的
Student st2;
st2.name = "李四";
}
}
enum Sex {
male,
female
}
struct Student {
public string name;
public float height;
public float weight;
public Sex sex;
public Student(string name, float height, float weight, Sex sex) {
this.name = name;
this.height = height;
this.weight = weight;
this.sex = sex;
}
public void Eat() {
}
}
}
本文来自博客园,作者:坞中客,转载请注明原文链接:https://www.cnblogs.com/wuzhongke/p/16860661.html