在接口中怎样添加方法????在方法中还有一个参数
接口中只能是抽象方法,不能有具体实现,接口中的全局变量默认是常量。
interface 接口名
{
public void 方法名(int 参数);
}
添加方法后,示例对象也要实现这个方法
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修饰符,继承接口的类要实现接口的方法
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();
}
}