zoukankan      html  css  js  c++  java
  • c# 07 接口

    接口的实现方式和抽象类一样,都是用“:”表示即可,光实现的话":"后其他的接口以逗号隔开即可,如果此类不光实现接口还要有继承,那么继承必须跟在最前面

    测试类

    using System;
    
    namespace Demo3
    {
        class Program
        {
            static void Main(string[] args)
            {
                Apple apple = new Apple();
                Grape grape = new Grape();
                apple.describe().colourDescribe().tasteDescribe();
                grape.describe().colourDescribe().tasteDescribe();
    
            }
        }
    }

    测试结果:

    实现类:苹果

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Demo3
    {
        /// <summary>
        /// 苹果类
        /// </summary>
        public class Apple : Fruits<Apple>, Colour<Apple>, Taste<Apple>
        {
            public Apple colourDescribe()
            {
                Console.Write("颜色红红的");
                return this;
            }
    
            public override Apple describe()
            {
                Console.Write("我是一个大苹果");
                return this;
            }
    
            public Apple tasteDescribe()
            {
                Console.WriteLine("味道甜甜糯糯的");
                return this;
            }
        }
    }

    实现类:葡萄

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Demo3
    {
        /// <summary>
        /// 葡萄类
        /// </summary>
        public class Grape : Fruits<Grape>, Colour<Grape>, Taste<Grape>
        {
            public Grape colourDescribe()
            {
                Console.Write("紫色的颜色");
                return this;
            }
    
            public override Grape describe()
            {
                Console.Write("我是一串大葡萄");
                return this;
            }
    
            public Grape tasteDescribe()
            {
                Console.WriteLine("酸酸甜甜的味道");
                return this;
            }
        }
    }

    抽象类:抽象基类水果类

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Demo3
    {
         public abstract class Fruits<T>
        {
            /// <summary>
            ///描述
            /// </summary>
            public abstract T describe();
        }
    }

    接口:味道接口

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Demo3
    {
        /// <summary>
        /// 味道接口
        /// </summary>
        public interface Taste<T>
        {
            /// <summary>
            /// 味道描述
            /// </summary>
            T tasteDescribe();
        }
    }

    接口:颜色接口

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Demo3
    {
        /// <summary>
        /// 颜色接口
        /// </summary>
        interface Colour<T>
        {
            /// <summary>
            /// 颜色描述
            /// </summary>
            T colourDescribe();
        }
    }
  • 相关阅读:
    web中状态码及请求方式
    访问服务器时一直在转圈,等待localhost响应
    Address already in use: JVM_Bind 端口被占用的几个解决办法
    Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean)
    taotao商城
    Dubbo的学习
    taotao商城
    sql中有一些保留字,当你的字段名是它的保留字时,这个时候sql语句的字段不加``就会报错
    ssm学习的第一个demo---crm(4)
    ssm学习的第一个demo---crm(3)
  • 原文地址:https://www.cnblogs.com/li-yan-long/p/14005900.html
Copyright © 2011-2022 走看看