IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> C#基础知识+代码(三) -> 正文阅读

[开发工具]C#基础知识+代码(三)

1.静态类、静态方法、静态属性
2.抽象类
3.接口
4.向上转型和向下转型
请添加图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class YourClass//实例类可以定义静态方法
        private string aa;//实例成员
        public static string bb;//静态成员

        public YourClass( )
        {
            Console.WriteLine( "实例构造函数...." );
        }
        static YourClass( )
        {
            Console.WriteLine( "静态构造函数....");
        }
        public static void FunctionStatic()
        {            
            Console.WriteLine( "静态方法.....");//静态方法不能访问(但可以自定义)实例成员,也不能访问实例方法。但可以访问静态成员
        }
        public void FunctionInstance( )//实例方法可以访问类成员,包括静态和实例成员
        {
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {
            YourClass yc1=new YourClass();
            YourClass yc2=new YourClass();
            YourClass.FunctionStatic();//静态方法通过类名调用,除了构造函数在创建对象时执行
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public static class StaticClass//静态类的所有成员都必须是static
    {
    {
           
}

请添加图片描述
抽象类Graphic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public abstract  class Graphic//抽象类不能被实例化
    {
        protected double[ ] bianChange;//实例成员

        //产品设计说明书
        public abstract double GetLength( );//抽象方法一定要定义抽象类当中

        public abstract double GetArea( );

        public Graphic( int length )//实例方法
        {
            bianChange = new double[ length ];

        }

        public virtual void Show()
        {
            Console.WriteLine( "这是图形的虚方法.....");
        }
    }
}

实现类Circle.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public  class Circle:Graphic//字类要实现父类所有的抽象成员
    {
        public Circle( int length )
            : base( length )//访问父类构造函数
        {
            base.bianChange[ 0 ] = 10;
        }
        //计算面积
        public override double GetArea( )//重写抽象方法override
        {
            return base.bianChange[ 0 ] * base.bianChange[ 0 ] * Math.PI;
        }
        //计算周长
        public override double GetLength( )
        {
            return base.bianChange[ 0 ] * 2 * Math.PI;
        }

        public override void Show( )
        {
            Console.WriteLine( "圆形的重写方法.....");
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {           
            Circle circle = new Circle( 1 );
            double mj = circle.GetArea( );
            Console.WriteLine( "面积:{0}平方米" , mj );
        }
    }
}

请添加图片描述
接口是通过类实现的,如同产品要根据产品说明书进行设计实现。两者有关联但不相同。

接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public interface IUSB
    {
        void Write( );//接口成员默认为public,接口只能定义不能实现{}
        void Read( );
    }
}

实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class MP3:IUSB//实现接口所有方法
    {
        public void Write( )
        {
            Console.WriteLine( "下载歌曲");
        }
        public void Read( )
        {
            Console.WriteLine( "播放歌曲" );
        }
    }
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {
            IUSB usb = new MP3( );
            usb.Read( );//调用MP3类的Read方法
        }
    }
}

eg:
接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public interface IMobile
    {
        void Call( );//打电话
        void Message( );//发短信
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public interface INet
    {
        void Internet( );//上网
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public interface IIntellMobile//智能手机
    {
        void Pay( );//支付
        void Chat( ); //聊天
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public interface IShouJi : INet , IMobile , IIntellMobile//接口可多继承父接口
    {
    }
}

实现:可继承多个接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public abstract class Mobile: IMobile//抽象类继承加实现接口
    {
        public void Call( )
        {
            Console.WriteLine( "打电话....");
        }
        public void Message( )
        {
            Console.WriteLine( "发短信...." );
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class TraditionMobile:Mobile,INet//实现上网接口,先写父类,再写接口
    {
        public void Internet( )
        {
            Console.WriteLine( "网上冲浪.....");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class IntellMobile:Mobile,IShouJi//实现剩余的接口方法
    {
        public void Pay( )
        {
            Console.WriteLine("支付宝......");
        }
        public void Chat( )
        {
            Console.WriteLine( "聊天......");
        }
        public void Internet( )
        {
            Console.WriteLine( "网址冲浪....");
        }
    }
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {      
            IntellMobile im = new IntellMobile( );
            im.Chat( );
        }
    }
}

请添加图片描述

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-05-21 19:11:00  更:2022-05-21 19:13:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/21 11:22:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码