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 成员

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

静态类无法实例化

相关推荐
小卡车55517 分钟前
java反射、自定义注解的应用(自定义分页)
java
徐小黑ACG28 分钟前
Golang 基础05 结构体struct
开发语言·算法·golang
优橙教育1 小时前
零基础学AI应用开发要多久?3个月能到什么水平
服务器·开发语言·网络·php
BLUcoding1 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
SEO_juper1 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
yuniko-n1 小时前
【JUC】Lock 和 synchronzied 锁
java
西柚小萌新1 小时前
【LLM&&AI应用开发 八股文】--4.3.Agent智能体(下)
java·开发语言·数据库
北极有牛1 小时前
cpp学习笔记--常量指针
java·开发语言·算法
java资料站2 小时前
二、Spring AI Alibaba · ChatModel
java·windows·spring
半杯咖啡半行码2 小时前
C++ 编程与 STL 模板:从泛型编程到内存安全详解
开发语言·c++