赶紧好好学学自己的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. ElasticSearch 使用小结

    写在前面 要做个元数据服务,包括存储和查询.元数据除了一些基本字段外,其他格式是自由的,存储输入为一个JSON形式.比如下面是一个文件对象的元数据: { "name":" ...

  2. 【 js 基础 】【 源码学习 】柯里化和箭头函数

    最近在看 redux 的源码,代码结构很简单,主要就是6个文件,其中 index.js 负责将剩余5个文件中定义的方法 export 出来,其他5个文件各自负责一个方法的实现. 大部分代码比较简单,很 ...

  3. maven插件 按配置加载不同环境配置文件进行打包(maven-war-plugin)

    1.配置多种不同环境 如(本地local,开发dev,测试test 环境等) <profiles> <profile> <id>local</id> & ...

  4. promise的理解和应用

    老铁们,我又满血复活了,今天我准备来吹一波我对promise,如有错吴请直接指出,明白了吗?话不多说开始吧 首先我们需要知道啥叫promise,我问了问大佬,他说这个东西是 异步操作的同步代码(but ...

  5. OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境

    上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...

  6. [iOS] UICollectionView初始化滚动到中间的bug

    转载请保留地址wossoneri.com 问题 首先看一下我之前写的demo:link demo是封装了一个控件,直接在MainViewController的viewWillAppear里初始化,并且 ...

  7. 几个python函数

    迭代器 fun函数 过滤作用 s=['a', 'b', 'c'] def fun1(s): if s!='a': return s ret filter(fun1, str) print(ret)   ...

  8. SecureCRT使用问题记录

    1.破解版下载&安装 参考:https://bbs.feng.com/read-htm-tid-6939481.html 2.session导入 查看 SecureCRT-Preference ...

  9. Mongodb集群与分片 2

    前面我们介绍了简单的集群配置实例.在简单实例中,虽然MongoDB auto-Sharding解决了海量存储问题,和动态扩容问题,但是离我们在真实环境下面所需要的高可靠性和高可用性还有一定的距离. 下 ...

  10. Scala包的使用

    package big.data.analyse.scala.classes /** * Created by zhen on 2018/9/15. */ object Packages { def ...