[1]字符串字符反转
namespace 字符串字符反转
{
class Program
{
static void Main(string[] args)
{
//接收用户输入的一句英文,将其中的单词以反序输出。 "I love you"→"I evol uoy"
Console.WriteLine("请输入一句英文");
string str = Console.ReadLine();
//拆分字符串为单个单词
string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < words.Length; i++)
{
words[i] = FanZhuan(words[i]);
}
//单个单词-->字符串
str = String.Join(" ", words);
Console.WriteLine(str);
Console.ReadKey();
}
public static string FanZhuan(string str)
{
char[] chars = str.ToCharArray();
char temp = ' ';
for (int i = 0; i < chars.Length / 2; i++)
{
temp = chars[i];
chars[i] = chars[chars.Length - i - 1];
chars[chars.Length - i - 1] = temp;
}
return new string(chars);
}
}
}
[2]小鸭子案例
namespace 小鸭子案例
{
class Program
{
static void Main(string[] args)
{
Duck[] ducks = { new realDuck(), new woodDuck(), new rubberDuck() };
//ISwim[] Animal = {new Dog(), };
//IBark[] Barker;
foreach (Duck item in ducks)
{
//用多态,用父类实现多态
item.Swim();
if (item is realDuck||item is rubberDuck)
{
//就是要用多态,用接口实现多态
((IBark)item).Bark();
}
}
Console.ReadKey();
}
}
//只有两个单词 interface和借口名,没有public,默认internal
interface IBark
{
//用public修饰多余
void Bark();
}
interface ISwim
{
void Swim();
}
//动物也可以继承ISwim接口
public abstract class Animal : ISwim
{
public abstract void Swim();
}
//
public class Dog : Animal
{
public override void Swim()
{
Console.WriteLine("我是乖乖狗,我的泳姿很特别");
}
}
public abstract class Duck
{
public abstract void Swim();
}
public class woodDuck : Duck
{
public override void Swim()
{
Console.WriteLine("我是木头鸭子,但我游泳游的可好了");
}
}
public class realDuck : Duck, IBark
{
public override void Swim()
{
Console.WriteLine("我是真鸭子,天生会游泳");
}
public void Bark()
{
Console.WriteLine("我是真鸭子,嘎嘎嘎嘎嘎");
}
}
public class rubberDuck : Duck, IBark
{
public override void Swim()
{
Console.WriteLine("我是橡胶鸭子,但我会游泳,哈哈");
}
public void Bark()
{
Console.WriteLine("我是橡胶鸭子,吱吱吱吱吱吱叫");
}
}
}
[*]登记案例
namespace 登记案例
{
class Program
{
static void Main(string[] args)
{
American american = new American();
Chinese chinese = new Chinese();
Car car = new Car();
House house = new House();
IRegistration[] ireg = {american,chinese,car,house };
for (int i = 0; i < ireg.Length; i++)
{
ireg[i].Registration();
}
Console.ReadKey();
}
public static void DengJi(IRegistration ireg)
{
ireg.Registration();
}
}
interface IRegistration
{
void Registration();
}
public class Person
{
}
//两人
public class American : Person, IRegistration
{
public void Say()
{
Console.WriteLine("我是美国人");
}
public void Registration()
{
Console.WriteLine("美国人已经登记");
}
}
public class Chinese : Person, IRegistration
{
public void Say()
{
Console.WriteLine("我是中国人");
}
public void Registration()
{
Console.WriteLine("中国人已经登记");
}
}
//两物
public class Car : IRegistration
{
public void Registration()
{
Console.WriteLine("我的蝙蝠车已经登记");
}
}
public class House : IRegistration
{
public void Registration()
{
Console.WriteLine("我的房子已经登记");
}
}
}