赶紧好好学学自己的C#,,要不然要给做的东西说拜拜了,,,时间紧迫,,,真担心会食言.....................

在C#中以为只要类有构造方法,,,,原来结构也有

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 结构
{
class Program
{
public struct Rect
{
public double width;
public double hight;
public Rect(double x,double y)//构造方法
{
width = x;
hight = y;
}
public double Area()
{
return width * hight;
}
}
static void Main(string[] args)
{
Rect rect;
rect.width = 5;
rect.hight = 6;
Console.WriteLine(rect.Area()); Rect re = new Rect(2,3);//使用结构的构造方法
Console.WriteLine(re.Area()); Console.ReadKey();
}
}
}

get set

假如说定义一个变量,A,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace getset
{
class Program
{
public class class1
{
int A; public static void Main()
{
class1 cla = new class1();
cla.A = 10; Console.WriteLine(cla.A); Console.ReadKey();
} }
}
}

现在这个变量需要做一下限制,,,对其赋值时不能大于65536,,怎么在赋值的时候判断这个变量是不是大于了65536,如果大于了给点提示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace getset
{
class Program
{
public class class1
{
int a;
public int A
{
get
{
return a;
}
set
{
if (value >= 65536)
{
Console.WriteLine("This value greater than the 65536");
}
else
{
a = value;
}
}
}
public static void Main()
{
class1 cla = new class1();
cla.A = 666666;
Console.WriteLine(cla.A);
Console.ReadKey();
}
}
}
}

多个CS文件

命名空间不一样的

关于继承----继承者和被继承者里面的东西一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Inherit
{
public int val = ; public void print()
{
Console.WriteLine(val);
}
}
class Inherit_1 : Inherit
{
public int val = ; public void print()
{
Console.WriteLine(val);
}
}
class Program
{
static void Main(string[] args)
{
Inherit inher = new Inherit();
inher.print(); Inherit_1 inher_1 = new Inherit_1();
inher_1.print(); Console.ReadKey();
} }
}

会有一个提示

当派生类从基类继承时,它会获得基类的所有方法、字段、属性和事件。若要更改基类的数据和行为,您有两种选择:可以使用新的派生成员替换基成员,或者可以重写虚拟的基成员。

上面的基类(父类)没有虚方法,,,,,所以可以选择第一种,,,,,使用新的派生成员替换基类成员

使用新的派生成员替换基类的成员需要使用 new 关键字。如果基类定义了一个方法、字段或属性,则 new 关键字用于在派生类中创建该方法、字段或属性的新定义。new 关键字放置在要替换的类成员的返回类型之前

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Inherit
{
public int val = ; public void print()
{
Console.WriteLine(val);
}
}
class Inherit_1 : Inherit
{
public new int val = ;//只是多加了一个new public new void print()
{
Console.WriteLine(val);
}
}
class Program
{
static void Main(string[] args)
{
Inherit inher = new Inherit();
inher.print(); Inherit_1 inher_1 = new Inherit_1();
inher_1.print(); Console.ReadKey();
} }
}

其实结果一样,

一句话,如果子类继承了父类,子类和父类中有一个相同的方法,,,子类调用这个方法时默认先在子类中找,,,如果子类没有才会在父类中找

其实也可以这样写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Inherit
{
public int val = ; public void print()
{
Console.WriteLine(val);
}
}
class Inherit_1 : Inherit
{
public int val = ; public void print()
{
Console.WriteLine(val);
}
}
class Program
{
static void Main(string[] args)
{
Inherit_1 inher_1 = new Inherit_1();//子类
inher_1.print(); Inherit inher = (Inherit)inher_1;//父类--强制转换
inher.print(); Console.ReadKey();
} }
}
Inherit_1 inher_1 = new Inherit_1();//子类
inher_1.print();//调用的是子类的方法 Inherit inher = (Inherit)inher_1;//父类--将派生类的实例强制转换为基类的实例--然后赋值
inher.print();//调用的是父类的方法

为了使派生类的实例完全接替来自基类的类成员,基类必须将该成员声明为虚拟的。这是通过在该成员的返回类型之前添加 virtual 关键字来实现的。然后,派生类可以选择使用override 关键字而不是 new,将基类实现替换为它自己的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Inherit
{
public virtual void print()//父类中用virtual关键字说明这个方法是一个虚方法
{
Console.WriteLine("");
}
}
class Inherit_1 : Inherit
{
public override void print()//重写父类的虚方法
{
Console.WriteLine("");
}
}
class Program
{
static void Main(string[] args)
{
Inherit inher1 =new Inherit();//直接新建父类对象
inher1.print();//调用父类方法 Inherit_1 inher_1 = new Inherit_1();//新建子类对象
inher_1.print();//调用子类方法 Inherit inher = (Inherit)inher_1;//强制转换为父类.....还是不行的
inher.print();//还是调用子类方法 Console.ReadKey();
}
}
}

主要看打印

如果要停止这个虚拟方法,,,

让这个方法不是虚的了,,要用 sealed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class A
{
public virtual void print()//父类中用virtual关键字说明这个方法是一个虚方法
{
Console.WriteLine("");
}
}
class B : A
{
public override void print()//重写父类的虚方法
{
Console.WriteLine("");
}
}
class C : B
{
public override void print()//重写父类的虚方法
{
base.print();//已替换或重写某个方法或属性的派生类仍然可以使用基关键字访问基类的该方法或属性
//Console.WriteLine("789");
}
} class Program
{
static void Main(string[] args)
{
C c = new C();
c.print(); Console.ReadKey();
}
}
}

洗澡,吃饭....................

学习C#(一)的更多相关文章

  1. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  2. Angular2学习笔记(1)

    Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...

  3. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  4. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  5. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  6. Unity3d学习 制作地形

    这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...

  7. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  8. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

  9. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  10. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

随机推荐

  1. java队列Queue及阻塞队列

    java队列 接口Queue类在java.util包,定义了以下6个方法 详细查看官方文档https://docs.oracle.com/javase/7/docs/api/java/util/Que ...

  2. 怎么给SharePoint得视图设置多个过滤条件? How to set multiple complex filter conditions to the SharePoint list view ?

    平时会很容易遇到需要给视图设置复杂得过滤条件,如果是一两个条件还好多,如果条件超过四个,会比较麻烦,很容易会出现逻辑不清,或者没有按照你得意愿来过滤数据得问题. 解决方案: 设置计算列,calcula ...

  3. atitit.网络文件访问协议.unc smb nfs ftp http的区别

    atitit.网络文件访问协议.unc smb nfs ftp http的区别 1. 网络文件访问协议1 2. NETBios协议  2 3. SMB(Server Message Block)2 3 ...

  4. [Android] 实现简单的相机程序

    好久没写了,有些东西做过都快忘了,赶紧记一下. 现在来实现一个简单的相机程序. 原文地址http://www.cnblogs.com/rossoneri/p/4246134.html 当然需要的话可以 ...

  5. Charles抓取https请求

    最近公司将Windows产品的http请求,替换成https请求了,当https请求超过5次失败,就自动切换回http请求.测试时使用Charles抓包测试. 一.http抓包 http抓包比较简单, ...

  6. API接口安全设计(转)

    接口的安全性主要围绕Token.Timestamp和Sign三个机制展开设计,保证接口的数据不会被篡改和重复调用,下面具体来看: Token授权机制:用户使用用户名密码登录后服务器给客户端返回一个To ...

  7. 从ibd文件获取表空间id

    xtrabackup恢复过程中出现如下错误 InnoDB: Doing recovery: scanned up to log sequence number ( %) InnoDB: Doing r ...

  8. oracle like模糊查询简单用法

    like  用法介绍: 1.“_”:匹配单个任意字符 select * from bqh3 where name like '_崔'; 2.“%”:匹配0个或多个任意字符.但有三种情况如下: like ...

  9. Hadoop HBase概念学习系列之META表和ROOT表(六)

    在 HBase里的HRegion 里,谈过,HRegion是按照表名+开始/结束主键,即表名+主键范围来区分的.由于主键范围是连续的,所以一般用开始主键就可以表示相应的HRegion了. 不过,因为我 ...

  10. python 下 安装openCV

    安装openCV openCV是Intel 创建的计算机视觉库,用于计算机图像处理. 安装anaconda,在命令行中输入conda install cv2/opencv 报错汇总 网络连接问题 报错 ...