C# 1120抽象类 static




抽象类

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_Up
{
    public abstract class People
    {
        public abstract int age
        {
            get; set;
        }
        public abstract void work();
    }

    public class Student : People
    {
        private int studentId;
        private string studentName;
        private int _age;
        public override int age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }
        public Student(int id,string name)
        {
            studentId = id;
            studentName = name;
        }
        public override void work()
        {
            Console.WriteLine("编号:"+studentId+",姓名:"+studentName+",年龄:"+age+"ok");
        }

    
    
    } 

}
csharp 复制代码
namespace C_Up
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Course course = new Course();
            Course yuwen;//空指针
            course.CourseId = 2;
            // yuwen.CourseName = string;
            course.CourseName = "math";
            course.ShowCourse();

            Course cs
                =new Course();
            cs.CourseId = 3;
            cs.CourseName = "computer science";
            cs.ShowCourse();

            Student student=new Student(22,"xiaoli");
            student.age = 23;
            student.work();
        }
    }
}

总结

不能再抽象类外部声明抽象方法

static

静态属性、方法

csharp 复制代码
  Student student=new Student(22,"xiaoli");
  student.age = 23;
  student.work();
  //静态对象不能使用类的对象来访问 类名.属性名访问
  Game game=new Game();
  Game.T_GameCount = 1;
  Game.gameType = "Moba";
     game.AddGame();
csharp 复制代码
   public class Game
   {
       public static string gameType;
       public static int T_GameCount {  get; set; }
       public void AddGame()
       {
           Game.T_GameCount+=1;
       }
   }
csharp 复制代码
  public class Game
  {
      public static string gameType;
      public static int T_GameCount { get; set; }
      public int AddGame()
      {
          Game.T_GameCount += 1;
          return T_GameCount;
      }

      public static void ShowGame()
      {
          T_GameCount = 1;
          //AddGame();不能使用非静态方法
          string fuwuqi = "LT";
          Console.WriteLine("游戏数量:"+T_GameCount+"运营商:+"+fuwuqi);
      }

  }
csharp 复制代码
           //静态对象不能使用类的对象来访问 类名.属性名访问
           Game game=new Game();
           Game.T_GameCount = 1;
           Game.gameType = "Moba";
             int result =game.AddGame();
           Console.WriteLine("gameType:"+Game.gameType+",game.num:"+result);

          Game.ShowGame();

总结

static class 静态类

static 成员

静态方法不能调用普通对象,只能访问静态成员

静态类无法实例化

相关推荐
EXI-小洲17 分钟前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员1 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导1 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan1 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251491 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901701 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..2 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說2 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
QQ_21696290963 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
可乐鸡翅yeah_4 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线