using System;
namespace work2
{
class Program
{
static void Main(string[] args)
{
int i = 0;
string str;
Console.WriteLine("请输入整数i:");
str = Console.ReadLine();
i = int.Parse(str); //要输入int型需要转化
Console.Write("i*5=",i * 5);
Console.ReadKey();
}
}
}
运行结果:
【记录】C#的访问修饰符以及C#静态类、静态方法的使用
C#访问修饰符及静态类
19.10.14
今日份C#课上知识点
//教室这个输入法真的是用不惯,回去再写
string.Empty 只读 (不能set)
类库不会生成.exe
生成.dll 可被调用
调用前需要先引用,然后再用using
项目 即程序集
internal 范围 本程序集
如果不加的话,默认internal
子类访问父类方法 this. ……();
pravite < protected <internal < public
其中pravite和protected
protected internal 别的程序集的子类
partial Windows窗体应用程序中,用于隔离代码,自动合并
静态类无法被实例化,是密封的
sealed 密封,无法被继承
类中可有静态方法
静态构造函数,自动执行,仅执行一次
析构函数,先不管
readonly i
只能在声明和构造函数中赋值
为了和类区分,结构不能继承
new表达式要求在类型后有()、[]或{}。
控制台的 namespace HL19_10_14TestKnowlege 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibraryTestK; //调用类库前,先引用类库(添加引用后,再用using)
namespace HL19_10_14TestKnowlege
{
class Program
{
static void Main(string[] args)
{
Person person = new Person();
Time.getTime(); //静态类,只能直接用
Test1 t1 = new Test1();
t1.theCout();
Test1.cout(); //静态方法,也是只能直接调用
Console.ReadKey();
}
}
}
类库的 namespace ClassLibraryTestK 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//类库不会生成.exe,生成的是.dll
namespace ClassLibraryTestK
{
class Class1 //不写访问修饰符,默认为internal
{
}
internal class Class2 //范围:项目(程序集)
{
}
public class Person //范围:所有
{
public static string getName()
{
return string.Empty; //只读
}
}
public class Strdent:Person //这个就是Person的子类
{
string name;
protected string GetName() //protected好像不能用于类的前面 范围:类中和子类中可用
{
return name;
}
protected internal void SetName(string name) //范围:比protected多了跨程序集的子类中也可用
{
this.name = name;
}
}
public static class Time //静态类是无法被实例化的,是密封的,也无法被继承
{
static Time()
{
time = "1";
}
static string time = "forever"; //声明成员变量也要加上static
private static void setTime() //这个好像也是不能用于类 范围:类中
{
//this.time = "1"; //this在静态中无效
}
public static string getTime() //不能在静态类中声明实例成员
{
Console.WriteLine(time);
return time;
}
}
public sealed class Time2 //sealed的意思就是不能被继承
{
}
public class Test1
{
public static void cout() //普通类中的静态方法
{
Console.WriteLine(2);
}
public void theCout()
{
//this.cout(); //静态方法不能被实例化调用
cout();
}
}
}