问题描述
- 在接口中添加方法????我调不出来。。
-
using System;
interface IVehicle
{
public void start(int x);
public void stop(int x);
}
class Bike : IVehicle { }
class Bus : IVehicle { }
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();}
}
解决方案
''''using System;
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class Bus : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
using System;
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class Bus : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
}
}
'
'''
解决方案二:
''''using System;
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class Bus : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
}
}
解决方案三:
sorry 这个编辑器用的不习惯。
'using System;
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class Bus : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
}
}
解决方案四:
希望这次对了
using System;
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class Bus : IVehicle {
public void start(int x)
{
//...
}
public void stop(int x)
{
//...
}
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
}
}
解决方案五:
你的错误主要是两个
一个是,如果你的类实现了接口,必须定义接口定义的方法,并且方法名参数返回值要一样。(不考虑协变逆变,这个暂时你学不到)
一个是,你的接口定义方法不要public修饰了。
解决方案六:
interface IVehicle
{
void start(int x);
void stop(int x);
}
class Bike : IVehicle
{
public void start(int x) { Console.WriteLine(x); }
public void stop(int x) { Console.WriteLine(x); }
}
class Bus : IVehicle
{
public void start(int x) { Console.WriteLine(x); }
public void stop(int x) { Console.WriteLine(x); }
}
class interfaceDemo
{
public static void main()
{
Bike b1 = new Bike();
Bus b2 = new Bus();
}
}
接口定义不能加public,private修饰符,继承接口的类要实现接口的方法
时间: 2024-10-03 16:08:16