首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

大家帮忙找上这6道C#的错,再给点解释 多谢哈

2013-01-02 
大家帮忙找下这6道C#的错,再给点解释谢谢哈1.******************************************************cla

大家帮忙找下这6道C#的错,再给点解释 谢谢哈
1.******************************************************
class Program
    {
        public static void Main()
        {
            A t = new A();
            int x, y;
            x = 5; y = 8;
            t.S(x,y);
            Console.WriteLine("x={0},y={1}",x,y); 
        }
    }
    class A
    {
        public void  S(int a, int b)
        {
            int temp;
            temp = a; a = b; b = temp;
        }
    }
2.******************************************************
class A
    {
        static int n;
        public A()
        {
            n++;
        }
        public void B()
        {
            Console.WriteLine(n);
        }
        public static void Main()
        {
            B();
       }
3.******************************************************
    class A
    {
        public static void Main()
        {
            C d = new C();
        }
    }
    class B
    {
        public B()
        {
            Console.WriteLine("实例化B对象!");
        }
    }
    class C : B
    {
        public C()
        {
            Console.WriteLine("实例化Dog对象!");
        }
    }
4.******************************************************
class Program
     { 
         public static void Main()
         {


             Dog d = new Dog();
             Test t = new Test();
             t.Eat(d);
         }
     }
     class Test
     {
         public void Eat(Animal a)
         {
             a.Eat();
         }
     }
     class Animal
     {
         public void Eat()
         {
             Console.WriteLine("Eat Something!");
         } 
     }
     class Dog : Animal
     {
         public void Eat()
         {
             Console.WriteLine("Eat Bone!");
         }
     }
5.******************************************************
interface IUSB
     {
         public void Download(string file)
         { }
     }
  ********************************************
     class Mp3:IUSB
     {
         public void Play(string file)
         {
             Console.WriteLine("播放{0}",file);
         }
     }
6.*****************************************************
class Program
     {   
         public static void Main()
         {
             Mp3 mp3 = new Mp3();
             mp3.Download("test.mp3");       
         }
     }
     interface IUSB
     {
         void Download(string file);
     }
     interface IBluetooth
     {
         void Download(string file);
     }
     class Mp3:IUSB,IBluetooth
     {
         void IUSB.Download(string file)
         {


             Console.WriteLine("用USB下载{0}", file);
         }
         void IBluetooth.Download(string file)
         {
             Console.WriteLine("用Bluetooth下载{0}", file);
         }    
     }
7.*****************************************************
     class Program
     {   
          public static void Main()
         {
             Math math = new Math();
             Call obj;
             obj = new Call(math.Add);
             int r=obj(3, 5);
             Console.WriteLine(r);
         }
     }
     public delegate int Call(int a,int b);
     class Math
     {
         public int Add(int a, int b)
         {
             Console.WriteLine("Add Method");
             return a + b;
         }
         public int Sub(int a, int b)
         {
             Console.WriteLine("Sub Method");
             return a - b;
         }
     }
8.*****************************************************
class Program
{   
    public static void Main()
    {
        Album friends = new Album(3); // 创建一个容量为 3 的相册
        Photo first = new Photo("Jenn"); // 创建 3 张照片
        Photo second = new Photo("Smith");
        Photo third = new Photo("Mark");
        friends[0] = first; // 向相册加载照片
        friends[1] = second;
        friends[2] = third;
        Photo obj1Photo = friends[2]; // 按索引检索
        Console.WriteLine(obj1Photo.Title);
        Photo obj2Photo = friends["Jenn"]; // 按名称检索


        Console.WriteLine(obj2Photo.Title);
    }
}
class Photo
{
    string _title;
    public Photo(string title)
    {
        this._title = title;
    }
    public string Title
    {
        get
        {
            return _title;
        }
    }
}
class Album
{
    Photo[] photos;
    public Album(int capacity)
    {
        photos = new Photo[capacity];
    }
}
9.*****************************************************
class Program
        {   
         public static void Main()
         {
             ArrayList al = new ArrayList();
             A a=new A(3);al.Add(a);
             A b=new A(4);al.Add(b);
             A c=new A(5);al.Add(c);
             al.RemoveAt(0);
            al.RemoveAt(1);
             foreach (object obj in al)
             {
                 A aa = (A)obj;
                 Console.WriteLine(aa.no);
             }
         }
     }
     class A
     {
         public int no;
         public A(int no)
         {
             this.no = no;
         } 
     }




[解决办法]

第一题想输出x=8,y=5?
class Program
    {
        public static void Main()
        {
            A t = new A();
            int x, y;
            x = 5; y = 8;
            t.S(x, y);


            Console.WriteLine("x={0},y={1}", x, y);
            Console.Read();
        }
    }
    class A
    {
        public void S(int a, int b)
        {
            int temp;
            temp = a; a = b; b = temp;
        }
    } 
建议看下值传递与引用传递

第二题无法引用方法B的错误?
编译的时候已经提示了无法引用非静态的字段或属性,将方法B声明成静态的public static void B()
第三题什么错误?建议让别人改错的时候写上啥错误,和期待的正确结果
第四题想输出Eat Bone!?
这样写
public virtual void Eat()
public override void Eat()
第五题干什么的?
第六题:
class Program
    {
        public static void Main()
        {
            IUSB mp3 = new IUSBMp3();
            mp3.Download("test.mp3");
            IUSB mp32 = new IBluetoothMp3();
            mp3.Download("tt");
            Console.Read();
        }
    }
    interface IUSB
    {
        void Download(string file);
    }
    interface IBluetooth
    {
        void Download(string file);
    }
    class IUSBMp3 : IUSB
    {
        public void Download(string file)
        {
            Console.WriteLine("用USB下载{0}", file);
        }        
    }
    class IBluetoothMp3 : IUSB
    {
        public void Download(string file)
        {
            Console.WriteLine("用Bluetooth下载{0}", file);
        }
    }
第七题委托题?没接触过直接忽略嘿嘿
第八题:
class Program
    {
        public static void Main()
        {
            Album album = new Album(3); // 创建一个容量为 3 的相册 
            Photo[] friends = album.photos;
            Photo first = new Photo("Jenn"); // 创建 3 张照片 


            Photo second = new Photo("Smith");
            Photo third = new Photo("Mark");
            friends[0] = first; // 向相册加载照片 
            friends[1] = second;
            friends[2] = third; 
            
            Photo obj1Photo = friends[2]; // 按索引检索 
            Console.WriteLine(obj1Photo.Title);
            //下边的错误是因为数组不支持用名称检索
            Photo obj2Photo = friends["Jenn"]; // 按名称检索 
            Console.WriteLine(obj2Photo.Title);
            Console.Read();
        }
    }
    class Photo
    {
        string _title;
        public Photo(string title)
        {
            this._title = title;
        }
        public string Title
        {
            get
            {
                return _title;
            }
        }
    }
    class Album
    {
        public Photo[] photos;
        public Album(int capacity)
        {
            photos = new Photo[capacity];
        }
    } 
第九题:
 A a = new A(3); al.Add(a);
            A b = new A(4); al.Add(b);
            A c = new A(5); al.Add(c);
            al.RemoveAt(0);
            al.RemoveAt(1);


         al.RemoveAt(0);//删除a
            al.RemoveAt(1);//删除c
所以输出4你认为输出5是不是?编译正确不知道你所指的错误是什么


[解决办法]


看了下,补充楼上的
第4题,如果是写出输出什么,那么应该是Eat Something!!,因为函数没有被重写,还是调用基类的函数

第5题:接口的方法是不需要修饰符的,而且也不需要实现,所以正确写法是:


interface IUSB 
    { 
       void Download(string file);
    } 

第6题:这题考的是,如果接口含有相同方法,那么该如何去继承和调用
继承的时候,是不能加修饰符public的,只能是private,题目上写法是正确的,关键就是调用的时候他写错了,正确写法:
class Program 
    {  
        public static void Main() 
        { 
            Mp3 mp3 = new Mp3(); 
            ((IUSB)mp3).Download("test.mp3");//调用IUSB接口的方法
            ((IBluetooth)mp3).Download("test.mp3");//调用IBluetooth接口方法
        } 
    } 

第七题:这程序没问题,可能考的是输出什么,结果是:8  委托的用法

其余的Up

热点排行