A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 秦永辉 中级黑马   /  2012-6-28 15:33  /  1496 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Oneclass
  2.     {
  3.         public string str1 = "this is Oneclass";
  4.         public void print1( string value)
  5.         {
  6.             Console.WriteLine(value);
  7.         }
  8.     }
  9.     class Otherclass : Oneclass
  10.     {
  11.         public string str2 = "this is Otherclass";
  12.         public void print2(string value)
  13.         {
  14.             Console.WriteLine(value);
  15.         }
  16.     }
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             Otherclass c = new Otherclass();
  22.             c.print2(c.str1);
  23.             Console.ReadKey();
  24.         }
  25.     }
复制代码
我不明白为什么基类能有派生类的方法?求解

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

2 个回复

正序浏览
1,由于基类的字符串变量str1 类型为public,
2,程序执行c.print2(c.str1);会输出基类的信息”this is Oneclass“,
3,若c.print2(c.str1)修改为 c.print2(c.str2);试试看会得出不一样的结果。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
弄错了,实例类时类名写错了,我说基类不应该有派生类的方法的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马