完全小白,急求大神指点第一个小程序

wang1234587 2013-11-20 08:28:02
这是我的第一个程序,所以我的问题对于大神来讲可能是弱智型的或是不可理喻型的,但还望大神能够不吝赐教,指出错误,帮忙改正,谢了!!!

我的第一个程序是这样的,我想先定义一个抽象类Animal作为基类,基类里有两个域名字和类型一个Animal的构造函数,还有一个虚方法Getname;
Dog类是Animal的派生类,派生类有一个自己的构造函数,并重新覆盖了基类的虚方法Getname;
在主函数中,我实例化一个名为doudou的对象,然后调用Getname;

以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
class Program
{
abstract public calss Animal
{
public string A_name;
public string A_Type;
public Animal(string name)
{ A_name=name;
A_Type="动物";
}
public virtual string Getname(){
Console.WriteLine("当前的动物是{0},名字是{1}",A_name,A_Type);
return A_name;}
}
class Dog:Animal{
public Dog(){
A_Type="狗";}
public override string Getname(){
base.Getname();
}
}
static void Main(string[] args)
{
Animal doudou=new Dog("豆豆");
doudou.Getname();
}
}
}



以下是错误的信息:
错误 1 应为 get 或 set 访问器 第12行
错误 2 应输入 } 第11行
错误 3 方法必须具有返回类型 第14行
错误 4 应输入 class、delegate、enum、interface 或 struct 第29行
错误 5 应输入标识符 第29行
错误 6 应输入 class、delegate、enum、interface 或 struct 第29行
错误 7 应输入 class、delegate、enum、interface 或 struct C 第31行
错误 8 应输入类型、命名空间定义或文件尾 第34行
错误 9 应输入类型、命名空间定义或文件尾 第35行



哎,真的很想弄明白怎么回事,大神啊,救救我吧
...全文
121 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang1234587 2013-11-20
  • 打赏
  • 举报
回复
引用 6 楼 junlinfushi 的回复:
具体错误已经指出,希望你写代码还是细心点,单词拼错就是自己问题。编译出错,要学会查找错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    abstract public class Animal  //首先,你那里的class类都拼错了
    {
        public string A_name;
        public string A_Type;

        public Animal()  //加上0参数的构造函数
        { }

        public Animal(string name)
        {
            A_name = name;
            A_Type = "动物";
        }

        public virtual string Getname()
        {
            Console.WriteLine("当前的动物是{0},名字是{1}", A_name, A_Type);
            return this.A_name;
        }
    }

    class Dog : Animal
    {
        public Dog() //父类没有0参数的构造函数,子类出现0参数构造函数会报错
        {
            A_Type = "狗";
        }
        public Dog(string name) //必须有一个参数的构造函数,否则Animal doudou = new Dog("豆豆"); 这就错了
        {
            A_Type = name;
        }
        public override string Getname()
        {
            return base.Getname();   //这里必须加上return 否则,你只是取得base.Getname的值,却没返回
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            Animal doudou = new Dog("豆豆");  
            doudou.Getname();
        }
    }
}
十分感谢啊,您指出的这些细节错误(比如子类0参数构造函数的前提必须是父类有0参数构造函数,这在我的书本上没有的。。。还是谢谢啦
Regan-lin 2013-11-20
  • 打赏
  • 举报
回复
你有仔细看过书的么?还是闻下书就来敲了?你继承的构造函数的有参数,你子类都没传参
Regan-lin 2013-11-20
  • 打赏
  • 举报
回复
我都知道你这8章书是怎呢看的,abstract public calss Animal你见过这样的抽象类?
junlinfushi 2013-11-20
  • 打赏
  • 举报
回复
具体错误已经指出,希望你写代码还是细心点,单词拼错就是自己问题。编译出错,要学会查找错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    abstract public class Animal  //首先,你那里的class类都拼错了
    {
        public string A_name;
        public string A_Type;

        public Animal()  //加上0参数的构造函数
        { }

        public Animal(string name)
        {
            A_name = name;
            A_Type = "动物";
        }

        public virtual string Getname()
        {
            Console.WriteLine("当前的动物是{0},名字是{1}", A_name, A_Type);
            return this.A_name;
        }
    }

    class Dog : Animal
    {
        public Dog() //父类没有0参数的构造函数,子类出现0参数构造函数会报错
        {
            A_Type = "狗";
        }
        public Dog(string name) //必须有一个参数的构造函数,否则Animal doudou = new Dog("豆豆"); 这就错了
        {
            A_Type = name;
        }
        public override string Getname()
        {
            return base.Getname();   //这里必须加上return 否则,你只是取得base.Getname的值,却没返回
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            Animal doudou = new Dog("豆豆");  
            doudou.Getname();
        }
    }
}
youzelin 2013-11-20
  • 打赏
  • 举报
回复
Dog 好像没有 带参数的构造函数吧
wang1234587 2013-11-20
  • 打赏
  • 举报
回复
引用 3 楼 junlinfushi 的回复:
引用 2 楼 wang1234587 的回复:
引用 1 楼 junlinfushi 的回复:
看书去吧
大哥啊,我知道我很小白,您攻击我、骂我无所谓,可是您指点了大方向让我去看书之后,能不能给我讲讲我这个程序呢?我的C#书已经看了一半了,看了八个章节了,总得调试小程序吧
你确定你会结贴吗?是的话,我乐意帮你
我去,功利心昭然啊,我会结贴。。。 现在我找到了第一个大错误了,我定义类的时候,在class program里定义的,应该在外面。 我改正了这个错误之后,还是有三个错误,您给看下啊 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication10 { abstract public class Animal { public string A_name; public string A_Type; public Animal(string name) { A_name=name; A_Type="动物"; } public virtual string Getname(){ Console.WriteLine("当前的动物是{0},名字是{1}",A_name,A_Type); return A_name;} } class Dog:Animal{ public Dog(){ //错误 1 “ConsoleApplication10.Animal”不包含采用“0”参数的构造函数 A_Type="狗";} public override string Getname(){ //错误 2 “ConsoleApplication10.Dog.Getname()”: 并非所有的代码路径都返回值 base.Getname(); } } class Program { static void Main(string[] args) { Animal doudou=new Dog("豆豆"); //错误 3 “ConsoleApplication10.Dog”不包含采用“1”参数的构造函数 doudou.Getname(); } } }
junlinfushi 2013-11-20
  • 打赏
  • 举报
回复
引用 2 楼 wang1234587 的回复:
引用 1 楼 junlinfushi 的回复:
看书去吧
大哥啊,我知道我很小白,您攻击我、骂我无所谓,可是您指点了大方向让我去看书之后,能不能给我讲讲我这个程序呢?我的C#书已经看了一半了,看了八个章节了,总得调试小程序吧
你确定你会结贴吗?是的话,我乐意帮你
wang1234587 2013-11-20
  • 打赏
  • 举报
回复
引用 1 楼 junlinfushi 的回复:
看书去吧
大哥啊,我知道我很小白,您攻击我、骂我无所谓,可是您指点了大方向让我去看书之后,能不能给我讲讲我这个程序呢?我的C#书已经看了一半了,看了八个章节了,总得调试小程序吧
junlinfushi 2013-11-20
  • 打赏
  • 举报
回复
看书去吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧