|
06面向对象练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _06面向对象练习
{
class Program
{
static void Main(string[] args)
{
//作业:定义父亲类Father(姓lastName,财产property,血型bloodType),
//儿子Son类(玩游戏PlayGame方法),女儿Daughter类(跳舞Dance方法),
//调用父类构造函数(:base())给子类字段赋值
//Son son = new Son("张三",10m,"AB");
//son.PlayGame();
//son.SayHello();
//Daughter d = new Daughter("张梅梅", 100m, "O");
//d.SayHello();
//d.Dance();
//Console.ReadKey();
//作业:定义汽车类Vehicle属性(brand(品牌),color(颜色))方法run,
//子类卡车(Truck) 属性weight载重 方法拉货,轿车 (Car) 属性passenger载客数量 方法载客
//Truck truck = new Truck("解放牌卡车", "绿色", 30000);
//Car car = new Car("奔驰", "黑色", 5);
//truck.LaHuo();
//car.LaKe();
//Console.ReadKey();
}
}
///
/// 汽车的父类
///
public class Vehicle
{
public string Brand
{
get;
set;
}
public string Color
{
get;
set;
}
public void Run()
{
Console.WriteLine("我是汽车我会跑");
}
public Vehicle(string brand, string color)
{
this.Brand = brand;
this.Color = color;
}
}
public class Truck : Vehicle
{
public double Weight
{
get;
set;
}
public Truck(string brand, string color, double weight)
: base(brand, color)
{
this.Weight = weight;
}
public void LaHuo()
{
Console.WriteLine("我最多可以拉{0}KG货物",this.Weight);
}
}
public class Car : Vehicle
{
public int Passenger
{
get;
set;
}
public Car(string brand, string color, int passenger)
: base(brand, color)
{
this.Passenger = passenger;
}
public void LaKe()
{
Console.WriteLine("我最多可以拉{0}个人",this.Passenger);
}
}
public class Father
{
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private decimal _property;
public decimal Property
{
get { return _property; }
set { _property = value; }
}
private string _bloodType;
public string BloodType
{
get { return _bloodType; }
set { _bloodType = value; }
}
public Father(string lastName, decimal property, string bloodType)
{
this.LastName = lastName;
this.Property = property;
this.BloodType = bloodType;
}
public void SayHello()
{
Console.WriteLine("我叫{0},我有{1}元,我是{2}型血",this.LastName,this.Property,this.BloodType);
}
}
public class Son : Father
{
public Son(string lastName, decimal property, string bloodType)
: base(lastName, property, bloodType)
{
}
public void PlayGame()
{
Console.WriteLine("儿子会玩游戏");
}
}
public class Daughter : Father
{
public Daughter(string lastName, decimal property, string bloodType)
: base(lastName, property, bloodType)
{
}
public void Dance()
{
Console.WriteLine("女儿会跳舞");
}
}
}
07面向对象多态练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07面向对象多态练习
{
class Program
{
static void Main(string[] args)
{
//作业:员工类、部门经理类 程序猿类
//(部门经理也是员工,所以要继承自员工类。员工有上班打卡的方法。用类来模拟。
Employee emp = new Programmer();//new Manager();//new Employee();
emp.DaKa();
Console.ReadKey();
}
}
public class Employee
{
public virtual void DaKa()
{
Console.WriteLine("员工九点打卡");
}
}
public class Manager : Employee
{
public override void DaKa()
{
Console.WriteLine("经理十一点打卡");
}
}
public class Programmer : Employee
{
public override void DaKa()
{
Console.WriteLine("程序猿不打卡");
}
}
}
08面向对象多态练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08面向对象多态练习
{
class Program
{
static void Main(string[] args)
{
//作业:动物Animal 都有吃Eat和叫Bark的方法,狗Dog和猫Cat叫的方法不一样.
//父类中没有默认的实现所哟考虑用抽象方法。
Animal a = new Dog();//new Cat();
a.Bark();
a.Eat();
Console.ReadKey();
}
}
public abstract class Animal
{
public abstract void Eat();
public abstract void Bark();
}
public class Cat : Animal
{
public override void Eat()
{
Console.WriteLine("猫咪舔着吃");
}
public override void Bark()
{
Console.WriteLine("猫咪喵喵的叫");
}
}
public class Dog : Animal
{
public override void Bark()
{
Console.WriteLine("狗狗旺旺的叫");
}
public override void Eat()
{
Console.WriteLine("狗狗咬着吃");
}
}
}
09面向对象接口练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09面向对象接口练习
{
class Program
{
static void Main(string[] args)
{
//作业:鸟-麻雀sparrow['sp?r?u] ,鸵鸟ostrich['?strit?] ,
//企鹅penguin['pengwin] ,鹦鹉parrot['p?r?t]
//鸟能飞,鸵鸟,企鹅不能。。。你怎么办
IFlyable fly = new YingWu();// new QQ();//new YingWu();//new MaQue();
fly.Fly();
Console.ReadKey();
}
}
public interface IFlyable
{
void Fly();
}
public class Bird
{
public double Wings
{
get;
set;
}
public void SayHello()
{
Console.WriteLine("我是小鸟");
}
}
public class MaQue : Bird, IFlyable
{
public void Fly()
{
Console.WriteLine("麻雀会飞");
}
}
public class TuoNiao : Bird
{
}
public class QQ : Bird
{
}
public class YingWu : Bird, IFlyable
{
public void Fly()
{
Console.WriteLine("鹦鹉会飞");
}
}
}
10面向对象多态练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10面向对象多态练习
{
class Program
{
static void Main(string[] args)
{
//作业:橡皮rubber鸭子、木wood鸭子、真实的鸭子realduck。
//三个鸭子都会游泳,而橡皮鸭子和真实的鸭子都会叫,
//只是叫声不一样,橡皮鸭子“唧唧”叫,真实地鸭子“嘎嘎”叫,木鸭子不会叫.
//IBark bark = new RealDuck();//new XPDuck();
//bark.Bark();
XPDuck xp = new XPDuck();
MuDuck md = new MuDuck();
RealDuck rd = new RealDuck();
IBark bark = rd;
Duck duck = rd;
duck.Swim();
bark.Bark();
Console.ReadKey();
}
}
public class Duck
{
public virtual void Swim()
{
Console.WriteLine("是鸭子就会游泳");
}
}
public interface IBark
{
void Bark();
}
public class RealDuck : Duck, IBark
{
public void Bark()
{
Console.WriteLine("这的鸭子嘎嘎叫");
}
public override void Swim()
{
Console.WriteLine("真的鸭子会游泳");
}
}
public class MuDuck:Duck
{
public override void Swim()
{
Console.WriteLine("木头鸭子也会游泳");
}
}
public class XPDuck : Duck, IBark
{
public void Bark()
{
Console.WriteLine("橡皮鸭子唧唧叫");
}
public override void Swim()
{
Console.WriteLine("橡皮鸭子也会游泳");
}
}
}
11、抽象类练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _11_抽象类练习
{
class Program
{
static void Main(string[] args)
{
//作业:计算形状Shape(圆Circle,矩形Square ,正方形Rectangle)的面积、周长
Shape shape = new Square(5,7);//new Circle(5);
double area = shape.GetArea();
double perimeter = shape.GetPerimeter();
Console.WriteLine(area);
Console.WriteLine(perimeter);
Console.ReadKey();
}
}
public abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
public class Square : Shape
{
public double Height
{
get;
set;
}
public double Width
{
get;
set;
}
public Square(double height, double width)
{
this.Height = height;
this.Width = width;
}
public override double GetArea()
{
return this.Height * this.Width;
}
public override double GetPerimeter()
{
return (this.Height + this.Width) * 2;
}
}
public class Circle : Shape
{
public double R
{
get;
set;
}
public Circle(double r)
{
this.R = r;
}
public override double GetArea()
{
return Math.PI * this.R * this.R;
}
public override double GetPerimeter()
{
return 2 * Math.PI * this.R;
}
}
}
12字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _12字符串
{
class Program
{
static void Main(string[] args)
{
//string s1 = "张三";//GC
//s1 = null;
//if (string.IsNullOrEmpty(s1))
//{
// Console.WriteLine("是的");
//}
//else
//{
// Console.WriteLine("不是");
//}
//string s = "abcdefg";
//char[] chs = s.ToCharArray();
//chs[0] = 'b';
//s = new string(chs);
//Console.WriteLine(s);
////s[0]='b';
//Console.WriteLine(s[0]);
//Console.ReadKey();
//string s1 = "c#";
//string s2="C#";
//if (s1.Equals(s2,StringComparison.OrdinalIgnoreCase))
//{
// Console.WriteLine("相同");
//}
//else
//{
// Console.WriteLine("不相同");
//}
//Console.ReadKey();
//string s = "abcdefg";
//string sNew = s.Substring(3,1);
//Console.WriteLine(sNew);
//Console.ReadKey();
//string str = "abcd , , fd, fdafd [[ ---";
//string[] strNew = str.Split(new char[] { ',', ' ', '[', '-' }, StringSplitOptions.RemoveEmptyEntries);
//string[] names = { "张三", "李四", "王五", "赵六" };
//string s = string.Join("|",'c',true,3.13,100,1000m,"张三");
//Console.WriteLine(s);
//Console.ReadKey();
string str = "老赵伟大";
str = str.Replace("老赵", "**");
Console.WriteLine(str);
Console.ReadKey();
}
}
}
13、字符串练习
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _13_字符串练习
{
class Program
{
static void Main(string[] args)
{
//课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"
//string str = "abcdefg";
//char[] chs = new char[str.Length];
//foreach (var item in chs)
//{
// Console.WriteLine(item);
//}
//Console.ReadKey();
//Console.WriteLine("请输入一个字符串");
//string str = Console.ReadLine();
//char[] chs = new char[str.Length];
//int i = 0;
//foreach (var item in str)
//{
// chs = item;
// i++;
//}
//foreach (var item in chs)
//{
// Console.WriteLine(item);
//}
//Console.ReadKey();
//char[] chs = str.ToCharArray();
//for (int i = 0; i < chs.Length/2; i++)
//{
// char temp = chs;
// chs = chs[chs.Length - 1 - i];
// chs[chs.Length - 1 - i] = temp;
//}
//str = new string(chs);
//Console.WriteLine(str);
//Array.Reverse(chs);
//str = new string(chs);
//Console.WriteLine(str);
////课上练习2:接收用户输入的一句英文,将其中的单词以反序输出。 “I love you"→“i evol uoy"
//string str = "I Love You";// I evoL uoY
//string s2 = null;
//string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
//for (int i = 0; i < strNew.Length; i++)
//{
// string s = ProcessStr(strNew);
// s2 += s+" ";
//}
//Console.WriteLine(s2);
//Console.ReadKey();
//课上练习3:”2012年12月21日”从日期字符串中把年月日分别取出来,打印到控制台
//string date = "2012年12月21日";
//string[] dateNew = date.Split(new char[] { '年', '月', '日' }, StringSplitOptions.RemoveEmptyEntries);
//Console.WriteLine("{0}--{1}--{2}",dateNew[0],dateNew[1],dateNew[2]);
//Console.ReadKey();
// 练习5:123-456---789-----123-2把类似的字符串中重复符号去掉,
//既得到123-456-789-123-2. split()、
//string str = "123-456---789-----123-2";
//string[] strNew = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
//str = string.Join("-", strNew);
//Console.WriteLine(str);
//Console.ReadKey();
string[] str = File.ReadAllLines("员工工资.txt", Encoding.Default);
int max = int.MinValue;
int min = int.MaxValue;
int sum = 0;
for (int i = 0; i < str.Length; i++)
{
//张三,100
string[] strNew = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (int.Parse(strNew[1]) > max)
{
max = int.Parse(strNew[1]);
}
if (int.Parse(strNew[1]) < min)
{
min = int.Parse(strNew[1]);
}
sum += int.Parse(strNew[1]);
}
Console.WriteLine("工资最高是{0},最低是{1},平均薪资是{2}",max,min,sum/str.Length);
Console.ReadKey();
}
public static string ProcessStr(string str)
{
char[] chs = str.ToCharArray();
for (int i = 0; i < chs.Length / 2; i++)
{
char temp = chs;
chs = chs[chs.Length - 1 - i];
chs[chs.Length - 1 - i] = temp;
}
return new string(chs);
}
}
}
14、高效的StringBuilder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _14_高效的StringBuilder
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("张三");
sb.Append("李四");
sb.Append("王五");
sb.Insert(1, 123);
sb.Replace("李四", "颜世伟");
sb.Replace("颜世伟", "杀马特");
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
}
15、使用StringBuilder来拼接网页
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _15_使用StringBuilder来拼接网页
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("");
sb.Append("");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("");
sb.Append("");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("");
sb.Append("");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("星期一");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
webBrowser1.DocumentText = sb.ToString();
// webBrowser1.DocumentText
}
}
}
本文版权归原作者今日头条所有,如有侵权请联系管理员删除,原文地址:https://www.toutiao.com/a6717672910405763595/ |
|