//66,静态成员与非静态成员的访问方式
using System;
class Text
{
    public int x;
    public static int y;//静态数据成员
    void F()
    {
        x = 1;//正确,等价于this.x=1
        y = 1;//正确,等价于Text.y=1
    }
    static void G()//静态 函数 成员
    {
       // x = 1;错误,不能访问this.x
        y = 1;//正确,等价于Text.y=1
    }
}
class Program
{
    static void Main()
    {
        Text t = new Text();
        t.x = 1;
        //t.y = 1;错误,不能在类的实例中访问静态成员
        //Text.x = 1;错误,不能按类访问非静态成员
        Text.y = 1;
    }
}

//67,静态数据成员的定义和引用
using System;
class MyClass
{
    private int A,B,C;
    private static int sum;
    public MyClass(int a, int b, int c)
    {
        A = a;
        B = b;
        C = c;
        sum += A + B + C;
    }
    public void PrintMumber()
    {
        Console.WriteLine("{0};{1};{2}", A, B, C);
    }
    public int PrintSum(MyClass m)
    {
        return MyClass.sum;
    }
}
class MainClass
{
    static void Main()
    {
        MyClass M = new MyClass(3, 4, 5);
        MyClass N = new MyClass(5, 6, 7);
        M.PrintMumber();
        N.PrintMumber();
        //Console.WriteLine("{0};{1}", M.PrintMumber(M), N.PrintMumber(N));
        Console.WriteLine("{0};{1}", M.PrintSum(M), N.PrintSum(N));
    }
}
//68,使用静态方法访问静态数据成员
using System;
class M
{
    private int A;
    private static int B = 10;
    public M(int a)
    {
        A = a;
        B += a;
    }
    public static void fun(M m)
    {
        Console.WriteLine("A={0}", m.A);
        Console.WriteLine("B={0}", B);
    }
}
class MainClass
{
    static void Main()
    {
        M P = new M(6);//对于类M中的一般非静态数据成员A是通过对象m来访问的,而对于静态数据成员B,则采用类直接访问的方式
        M Q = new M(8);//对于静态方法成员的调用方式,在C#中只能用类来直接调用
        M.fun(P);// 6,16
        M.fun(Q);//8 18
    }
}
//69,用对象成员处理有关类的问题
using System;
class A
{
    char c;
    public A(char m)
    {
        c = m;
    }
    public void print()
    {
        Console.WriteLine("c={0}", c);
    }
}
class B
{
    int p;
    public B(char m,int n)
    {
        A a = new A(m);
        a.print();
        p = n;
    }
    ~B() { }
    public void print()
    {
        Console.WriteLine("p={0}", p);
    }
}
class MainClass
{
    static void Main()
    {
        B b = new B('M', 20);
        b.print();
    }
}
//70,结构和类的使用的对比之一
using System;
struct sPoint
{
    public int x, y;
    public sPoint(int a, int b)
    {
        x = a;
        y = b;
    }
}
class cPoint
{
    public int x, y;
    public cPoint(int a, int b)
    {
        x = a;
        y = b;
    }
}
class Program
{//在结构中将结构对象sp复制给新对象s后,开辟了各自的对象空间,即两个变量分别引用两个对象,每个变量都有自己的数据副本,操作一个
    //变量不影响其他变量,在类中将对象cp复制给新对象c后,共用同一对象空间,即两个变量只引用同一个对象,操作一个变量会影响另一个变量
    public static void Main()
    {
        sPoint sp = new sPoint(12, 12);
        sPoint s = sp;
        sp.x = 112;
        Console.WriteLine("结构:");
        Console.WriteLine("x的值:   " + s.x + "\ty的值: " + s.y);
        cPoint cp = new cPoint(12, 12);
        cPoint c = cp;
        cp.x = 112;
        Console.WriteLine("类:");
        Console.WriteLine("x的值:   " + c.x + "\ty的值:" + c.y);
    }
}
//71,结构和类的使用对比之二
using System;
class ClassPoint
{
    public int x, y;
    public ClassPoint(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
struct StructPoint
{
    public int x, y;
    public StructPoint(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
class Program
{//程序创建并初始化了一个含有10个点的数组,对于作为类的实现的Point,出现了11个实例对象,其中数组生命需要一个对象,他的10个元素每个都需要创建一个对象,
    //然而用结构体来实现,只需要创建一个对象,如果在类中也像结构中一样,把“cp[i]=new ClassPoint”一句删去,系统会报告“未处理的异常,未将对象引用设置到对象的
    //的实例,"可见,在类中必须为每个元素创建一个对象的实例,而在结构中却不需要,如果数组很大,用类和结构在处理有诸如“点”之类的问题的执行效率上,差别是非常大的
    /*结构和类的差别:1,结构是值类型,他在栈区分配地址。类是引用类型,他在堆区分配地址 2,结构成员默认的访问级别是public,而类成员默认访问级别是private
      3,结构不能作为基类型,类可以作为基类型 4,结构无析构函数,类有析构函数*/
    static void Main()
    {
        ClassPoint[] cp = new ClassPoint[10];
        for(int i=0;i<cp.Length;i++)
        {
            cp[i] = new ClassPoint(i, i);
            Console.WriteLine("({0},{1})", cp[i].x, cp[i].y);
        }
        Console.WriteLine();
        StructPoint[] sp = new StructPoint[10];
        for (int i = 0; i < sp.Length; i++)
        {
            Console.Write("({0},{1})", sp[i].x, sp[i].y);
        }
        Console.WriteLine();
    }
}
//72,枚举类型
using System;
class Program
{
    enum Color { black, blue, yellow = 14, white };
    public static void Main()
    {
        int a1 = (int)Color.black;
        int a2 = (int)Color.blue;
        int a3 = (int)Color.yellow;
        int a4 = (int)Color.white;
        Console.WriteLine("Black:{0}", a1);
        Console.WriteLine("blue:{0}", a2);
        Console.WriteLine("yellow:{0}", a3);
        Console.WriteLine("white:{0}", a4);
    }
}
//73,运算符重载
using System;
class Point
{
    public int x = 0;
    public int y = 0;
    public Point(int a, int b)
    {
        x = a;
        y = b;
    }
    public void Print1()
    {
        Console.WriteLine("第一个矢量的端点坐标是:({0},{1})", x, y);
    }
    public void Print2()
    {
        Console.WriteLine("第二个矢量的端点坐标是:({0},{1})", x, y);
    }
    public static Point operator +(Point a, Point b)
    {
        return new Point(a.x + b.x, a.y + b.y);
    }
}
class Program
{
    public static void Main()
    {
        Point a = new Point(3, 2);
        Point b = new Point(1, 5);
        Point c = a + b;
        a.Print1();
        b.Print2();
        Console.WriteLine("两矢量的端点坐标是:" + '(' + c.x + ',' + c.y + ')');
    }
}
//74,throw:抛出异常,try:捕获异常,catch:处理异常
using System;
class Program
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("1/2={0:F2}", Div(1, 2));
            Console.WriteLine("5/3={0:F2}", Div(5,3));
            Console.WriteLine("5/0={0:F2}", Div(5,0));
            Console.WriteLine("10/3={0:F2}", Div(10,3));
        }
        catch(Exception)
        {
            Console.WriteLine("发现异常!");
            Console.WriteLine("被零抛出!\n在除法运算中除数不能为零!");
        }
        Console.WriteLine("主函数结束.");
    }
    public static double Div(double a, double b)
    {
        if (b == 0)
            throw new Exception();
        return a / b;
    }
}
//75,异常情况
using System;
class Program
{
    public static void Main()
    {
        Console.Write("请输入编号:");
        string number = Console.ReadLine();
        Console.Write("请输入工资:");
        double earnings = Convert.ToDouble(Console.ReadLine());
        Console.Write("请输入年龄:");
        int age = Convert.ToInt32(Console.ReadLine());
        int f = test(number);
        int g = test(earnings);
        int h = test(age);
        if (f == 1 && g == 1 && h == 1)
            Console.WriteLine("\n编号:{0}\n工资:{1:C2}\n年龄:{2}", number, earnings, age);
    }
    public static int test(string p)
    {
        int f = 1;
        try
        {
            if (p[0] >= '0' && p[0] <= '9')
                throw new Exception();
        }
        catch (Exception)
        {
            f = 0;
            Console.WriteLine("编号错误:{0}", p[0]);
        }
        return f;
    }
    public static int test(double d)
    {
        int g = 1;
        try
        {
            if (d < 0 || d > 20000)
                throw new Exception();
        }
        catch (Exception)
        {
            g = 0;
            Console.WriteLine("工资错误:{0}", d);
        }
        return g;
    }
    public static int test(int a)
    {
        int w = 1;
        try
        {
            if (a < 18 || a > 60)
                throw new Exception();
        }
        catch (Exception)
        {
            w = 0;
            Console.WriteLine("年龄错误:{0}", a);
        }
        return w;
    }
}

<C#任务导引教程>练习八的更多相关文章

  1. Senparc.Weixin.MP SDK 微信公众平台开发教程(八):通用接口说明

    一.基础说明 这里说的“通用接口(CommonAPIs)”是使用微信公众账号一系列高级功能的必备验证功能(应用于开发模式). 我们通过微信后台唯一的凭证,向通用接口发出请求,得到访问令牌(Access ...

  2. Docker入门教程(八)Docker Remote API

    Docker入门教程(八)Docker Remote API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第八篇,重点介绍了Docker Remote ...

  3. 微信公众平台开发教程(八)Session处理

    微信公众平台开发教程(八)Session处理 在微信窗口,输入的信息有限,我们需要将一些信息分多次请求. 比如:在进行用户绑定时,我们需要输入用户的相关信息,比如:用户名.密码,或者姓名.电话号码,服 ...

  4. HMM 自学教程(八)总结

    本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在HMM 学习最佳范例,这是针对国外网站上一个 HMM 教程的翻译,作者功底很深,翻译得很精彩,且在 ...

  5. IOS编程教程(八):在你的应用程序添加启动画面

    IOS编程教程(八):在你的应用程序添加启动画面   虽然你可能认为你需要编写闪屏的代码,苹果已经可以非常轻松地把它做在Xcode中.不需要任何编码.你只需要做的是设置一些配置. 什么是闪屏 对于那些 ...

  6. Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

    UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...

  7. Java NIO系列教程(八)JDK AIO编程

    目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...

  8. iOS 11开发教程(八)定制iOS11应用程序图标

    iOS 11开发教程(八)定制iOS11应用程序图标 在图1.9中可以看到应用程序的图标是网状白色图像,它是iOS模拟器上的应用程序默认的图标.这个图标是可以进行改变的.以下就来实现在iOS模拟器上将 ...

  9. ComicEnhancerPro 系列教程十八:JPG文件长度与质量

    作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十八:JPG文件长度 ...

随机推荐

  1. 题解 CF468C Hack it!

    题目传送门 Description 设 \(f(i)\) 表示 \(i\) 的数码只和,给出 \(a\),求出 \(l,r\) 使得 \(\sum_{i=l}^{r} f(i)\equiv 0\pmo ...

  2. mysql join语句的执行流程是怎么样的

    mysql join语句的执行流程是怎么样的 join语句是使用十分频繁的sql语句,同样结果的join语句,写法不同会有非常大的性能差距. select * from t1 straight_joi ...

  3. NX CAM 读取加工参数

    '取加工几何试图程序组 Function GetGemoGroup_Name(ByVal camObjectTag As NXOpen.Tag) As String Dim theGemoGroupT ...

  4. ORA-19815: WARNING: db_recovery_file_dest_size闪回区爆满问题处理

    问题描述:有一个数据库起不来了,根据层层排查,是因为归档设置在了闪回区,文件的大小已经超出了闪回区限制.最后直接给数据库拖挂 环境:windows server2012 , oracle 19c,单机 ...

  5. NOIP模拟83(多校16)

    前言 CSP之后第一次模拟赛,感觉考的一般. 不得不吐槽多校联测 OJ 上的评测机是真的慢... T1 树上的数 解题思路 感觉自己思维有些固化了,一看题目就感觉是线段树. 考完之后才想起来这玩意直接 ...

  6. Go 语言实现 gRPC 的发布订阅模式,REST 接口和超时控制

    原文链接: 测试小姐姐问我 gRPC 怎么用,我直接把这篇文章甩给了她 上篇文章 gRPC,爆赞 直接爆了,内容主要包括:简单的 gRPC 服务,流处理模式,验证器,Token 认证和证书认证. 在多 ...

  7. [对对子队]Beta阶段项目展示博客

    Beta阶段项目展示博客 1 团队成员的简介和个人博客地址 成员 头像 岗位 博客 个人介绍 黄贤昊 PM 17373253 喜欢玩游戏和做游戏,项目经验基本都和游戏相关,擅长摸鱼,偶尔敬业. 吴桐雨 ...

  8. poi实现生成下拉选

    在我们日常开发中,经常需要使用poi操作excel文件,现在就简单介绍一下在poi中是如何生成下拉选的. 1.创建workbook 2.创建数据约束 3.设置数据的有效性 @Test public v ...

  9. 大神教零基础入门如何快速高效的学习c语言开发

    零基础如果更快更好的入门C语言,如何在枯燥的学习中找到属于自己的兴趣,如果把学习当成一种事务性的那以后的学习将会很难有更深入的进步,如果带着乐趣来完成学习那将越学越有意思这样才会让你有想要更深入学习的 ...

  10. 决策树 机器学习,西瓜书p80 表4.2 使用信息增益生成决策树及后剪枝

    使用信息增益构造决策树,完成后剪枝 目录 使用信息增益构造决策树,完成后剪枝 1 构造决策树 1 根结点的选择 色泽 信息增益 根蒂 信息增益 敲声 信息增益 纹理 信息增益 脐部 信息增益 触感 信 ...