一:
using System;
//类 class,是面向对象程序设计(oop),实现信息封装的基础
//类是一种用户自定义类型,简称类类型
//每个类包含一种数据说明和一组数据操作或传递消息的函数。类的实例称为对象。
//类的体系按照一定的逻辑
namespace 第二周_关于类
{ //信息封装,属性,方法(可以不是同时拥有)
class Box
{
public float length;
public float breadth;
public float height;
public Box(float length,float breadth,float height)//自定义构造函数
{
this.length = length;
this.breadth = breadth;
this.height = height;
}
public float GetVolume(float len,float bread,float height)
{
return length * breadth * height;
}
};
class Bottle
{
public float weight;
public int Volume;
public float height;//只有数据。可以和struct比较
public Material mat;//层层嵌套
};
class Material
{
}
class Program
{
static void Main(string[] args)
{
// Console.WriteLine("Hello World!");
//Box box1 = new Box();//默认会设置一个构造函数(如果自己构造了,则默认构造函数会报错)
Box box1 = new Box(1.0f, 2.0f, 3.0f);
Console.WriteLine(box1.length);
Console.WriteLine(box1.breadth);
Console.WriteLine(box1.height);
float p = box1.GetVolume(box1.length, box1.breadth, box1.height);
Console.WriteLine(p);
}
}
}
二.
using System;
class q
{
}
namespace 第二周_析构函数
{
class Line//和q类相比,只是写在不同的命名空间
{
//public class b//可嵌套
//{
// public static int m = 8;
//}
private float len;
public Line()
{
Console.WriteLine("对象已构建");
}
~Line()//析构函数无所谓私有,公有,因为必须有
{
Console.WriteLine("对象已删除");
}
public void SetLength(float length)
{
this.len = length;
}
public float Getlength()
{
return len;
}
}
class Program
{
static void Main(string[] args)
{
// Console.WriteLine("Hello World!");
// class k//类不可以写在这里,行为里的对象。逻辑错误,函数看成一种行为,这里是main函数
//{ }
//理解命名空间
//System.Console.WriteLine("Hello World!");
//第二周_析构函数.a.b.m = 6;//可以理解为代码进行分类
Line line = new Line();
line.SetLength(1.0f);
Console.WriteLine("线条的长度:"+line.Getlength());
//没有析构函数调用,这是由于编辑器的问题,老版是有析构的
}
}
}
三:
using System;
namespace 第二周_类的商业探究
{
//索引中既有set又有get,属性:代表可读可写,只有get代表只读属性
//接口实际是方法的集合,接口可以多重继承
public interface IMyIterface//外面public里面全是public
{
//void Function01();
//void Function02();
void Function01();//但是只要下面打一个括号就实现了,无论是否为空函数
};
public class livingThing : IMyIterface//如果继承接口,但是接口中的函数没有实现,则会报错
{
public void Function01()//以下是编辑器自动实现的接口。可以手写
{
throw new NotImplementedException();//抛出新的构造函数
}
};
//继承就是为了重复使用代码,c#中类不允许多重继承,
public class Animal:livingThing
{
public void EatFood()
{
}
public void Walk()
{
}
public void sleep()
{
}
};
public class Cat:Animal//继承了animal就可以直接使用animal里面的东西
{
};
public class Dog:Animal
{
};
class Program
{
static void Main(string[] args)
{
int[] a = new int[3] {1,2,3 };
double[] b = new double[] { 1f, 2f, 3d };//可以装精度比double低的
Console.WriteLine("Hello World!");
}
}
//建一个unity文件,一直追溯到根类,万类皆来自object类,一生二,二生三,三生万物
}
原文:https://www.cnblogs.com/Nicela/p/13079607.html