using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorsApplication
{
public class Number
{
public Number()
{
}
public int FindMax(int a, int b)
{
int result;
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
public static void Print()
{
Console.WriteLine("hello world");
}
public int fab(int n)
{
return n==1?1:n * fab(n - 1);
}
public void swapValue1(ref int x,ref int y)
{
Console.WriteLine("before swap x={0},y={1}",x, y);
int temp;
temp = x;
x = y;
y = temp;
Console.WriteLine("before swap x={0},y={1}", x, y);
}
public void getVal(out int x,out int y)
{//通过此函数给传参设置值
x = 100;
y = 200;
Console.WriteLine("after getValx={0},y={1}", x, y);
}
}
}
using System;
namespace OperatorsApplication
{
public class Program
{
static void Main(string[] args)
{
//Operator();
//Console.WriteLine("32:{0}",isPower(32));
Number number = new Number();
//调用类的非静态成员方法,必须使用类的实例来调用
int r = number.FindMax(3, 5);
Console.WriteLine("max={0}", r);
//调用类的静态方法,必须使用类名来调用
Number.Print();
int a = 3;
int b = 5;
number.swapValue1(ref b,ref a);
int a1;
int a2;
number.getVal(out a1,out a2);
Console.WriteLine("after getVal a1={0},a2={1}", a1, a2);
}
static void Operator()
{
//+ - * /数组运算符
//% 取余 模
int a = 90;
int b = 20;
Console.WriteLine("a%b={0}", a % b);
//++ -- 自增自减运算符 a++先运算后自增 ++a先自增后运算
//关系运算符== < <= > >= !=
if (a == b)
{
Console.WriteLine("a=b");
}
else
{
Console.WriteLine("a!=b");
}
//逻辑运算符 与&& 或|| 非!
bool x = true;
bool y = true;
if (x && y)
{
Console.WriteLine("均为真");
}
//位运算符
//按位与& 按位或| 取反~ 异或^(相同为0不同为1) 右移>> 左移<<
int aa = 60;//0011 1100
int bb = 13;//0000 1101
Console.WriteLine("aa={0}", Convert.ToString(aa, 2));//打印二进制
Console.WriteLine("aa&bb={0}", Convert.ToString(aa & bb, 2));
Console.WriteLine("aa|bb={0}", Convert.ToString(aa | bb, 2));
Console.WriteLine("~bb={0}", Convert.ToString(~ bb, 2));
Console.WriteLine("aa^bb={0}", Convert.ToString(aa^bb, 2));
Console.WriteLine("bb>>1={0}", Convert.ToString(bb>>1, 2));
Console.WriteLine("bb<<1={0}", Convert.ToString(bb << 1, 2));
}
//判断一个数是不是2的n次方
/*
2^n 10进制
123=1*10^2+2*10^1+3*10^0
1(n个0)=1*2^n
1000 2的n次方和此数-1得到的数相与 等于0,可以确定他是2的n次方
&
0111
0000=0
*/
public static bool isPower(int x)
{
if ((x&(x-1))==0)
{
return true;
}
else{
return false;
}
}
//找出数组只出现一次的数
//1001^0000=1001 x^x=0 x^0=x
public static int GetOne()
{
int[] nums = new int[] { 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 5 };
int temp = 0;
for(int i = 0; i < nums.Length; i++)
{
temp = temp ^ nums[i];
}
return temp;
}
//其他运算符
public static void OtherOperators()
{
int a,b;
a = 10;
b = 20;
int c = a == b ? a : b;//三目运算符:条件?满足:不满足
string str = "hello";
if(str is object)
{
Console.WriteLine("str是string的实例");//会输出
}
}
//流程控制
public static void Ctrl()
{
int a = 100;
if (a == 10)
{
}
else if(a == 20)
{
}
else if (a == 30)
{
}
else
{
}
switch (a)
{
case 10:
Console.WriteLine();
break;
case 20:
Console.WriteLine();
break;
default:
switch (a)
{
}
break;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP
{
internal class Box
{
//数据成员 类的成员变量
public double length;
public double width;
public double height;
//成员方法
public void setLength(double l)
{
length = l;
}
public void setWidth(double w)
{
width = w;
}
public void setHeight(double h)
{
height = h;
}
public double getVolume()
{
return width * length * height;
}
public Box(double length,double width,double height)//构造函数,多个构造函数(传参不同)叫重载
{
this.length = length;// this当前对象
this.width = width;
this.height = height;
boxNum++;
}
public Box()
{
boxNum++;
}
~Box()//析构函数:函数名和类名一样,在c++中很重要
{
//当对象销毁时调用此函数,释放资源
}
//静态变量是全局变量,生存周期为程序的生存周期,所有实例化对象共享一个变量,非静态变量是new的对象用自己的
public static int boxNum;//静态变量,记录new出多少个实例
//静态方法
public static void showBoxNum()//静态方法
{
Console.WriteLine(boxNum);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP
{
//模拟数据操作
internal interface MyInterface//接口
{
/// <summary>
/// 添加数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
int add(int data);
/// <summary>
/// 修改数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
int update(int data);
/// <summary>
/// 删除数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
int remove(int data);
/// <summary>
/// 查询数据
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
List<int> query(string where);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP
{
internal class MySQL:MyInterface
{
public MySQL()
{
}
public int add(int data)
{
Console.WriteLine("往 MySQL中添加数据{0}", data);
return 1;
}
public List<int> query(string where)
{
Console.WriteLine("往 MySQL中查询数据{0}", where);
return new List<int>();
}
public int remove(int data)
{
Console.WriteLine("往 MySQL中删除数据{0}", data);
return 1;
}
public int update(int data)
{
Console.WriteLine("往 MySQL中更改数据{0}", data);
return 1;
}
}
}
namespace OOP
{
/// <summary>
/// 面向对象的三个特点:
/// 1.封装
/// 2.继承
/// 3.多态
/// </summary>
internal class Program
{
public static void DatabaseOperate(MyInterface myInterface)
{
myInterface.add(10);
}
static void Main(string[] args)
{
//Box box = new Box();
//box.setHeight(10);
//box.setWidth(20);
//box.setLength(30);
Box box = new Box(1,2,3);
Console.WriteLine(box.getVolume());//体积
Box box1 = new Box();
//静态成员函数,要通过类名来访问
Box.showBoxNum();
DatabaseOperate(new MySQL());
DatabaseOperate(new SQLServer());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace OOP
{
internal class SQLServer: MyInterface//应用MyInterface接口,接口有哪些方法需实现接口的所有方法
{
public int add(int data)
{
Console.WriteLine("往SQLServer中添加数据{0}",data);
return 1;
}
public List<int> query(string where)
{
Console.WriteLine("往SQLServer中查询数据{0}", where);
return new List<int>();
}
public int remove(int data)
{
Console.WriteLine("往SQLServer中删除数据{0}", data);
return 1;
}
public int update(int data)
{
Console.WriteLine("往SQLServer中更改数据{0}", data);
return 1;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{/// <summary>
/// 抽象类
/// </summary>
public abstract class AbsShape
{
public abstract double getArea();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{/// <summary>
/// 嵌套类
/// </summary>
internal class Container
{
public Nested nested;
public class Nested
{
public Nested()
{
}
}
public Container()
{
this.nested = new Nested();
}
public void NewNested()
{
this.nested = new Nested();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
internal interface Interface1
{
//接口方法:输出面积的70倍
double getCost(double are);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
public interface Interface2
{
//接口方法
void ParentInterfaceMethod();
}
public interface IMyInterface :
Interface2
{
//子接口中的方法
void MethodToImplement();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
internal class MyInterfaceimp : IMyInterface
{
public MyInterfaceimp()
{
}
public void MethodToImplement()
{
throw new NotImplementedException();
}
public void ParentInterfaceMethod()
{
throw new NotImplementedException();
}
}
}
namespace MultipleInherit
{/// <summary>
/// 多重继承c#里不支持多重继承的,但是我们可以继承单一一个父类,然后实现多个接口
/// </summary>
internal class Program
{
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle(7, 5);
double area = rectangle.getArea();
Console.WriteLine("矩形面积为:{0}", area);
double result = rectangle.getCost(area);
Console.WriteLine("矩形面积为:{0}", result);
StaticClass.ShowMess();//使用静态类里的静态方法,使用方便相当于全局
StaticClass.num = 10;
Console.WriteLine(StaticClass.num);
//嵌套类
Container.Nested nested = new Container.Nested();
//抽象类 不能new
//AbsShape abs=new AbsShape();
Re shape = new Re();
shape.setLength(2);
shape.setWidth(1);
AbsShape abs = new Re();//这样定义只能访问抽象类中的方法,多态
Console.WriteLine(shape.getArea());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{/// <summary>
/// 该类继承自AbsShape
/// </summary>
internal class Re : AbsShape
{
private double length;
private double width;
public Re()
{
}
public void setLength(double length)
{
this.length = length;
}
public void setWidth(double width)
{
this.width = width;
}
public override double getArea()
{
return this.length * this.width;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
internal class Rectangle:Shape1,Interface1
{
public Rectangle(double length,double width) : base(length, width)
{
}
public double getArea()
{
return width * length;
}
public double getCost(double area)//实现接口
{
return area * 70;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{//私密封装类 不能被继承
internal sealed class sealedClass
{
public sealedClass()
{
}
}
//public class Child: sealedClass { }//会报错,因为密封类不能被继承
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
internal class Shape1
{
protected double length;
protected double width;
public Shape1(double length,double width)
{
this.length = length;
this.width = width;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInherit
{
public static class StaticClass
{
/// <summary>
/// 静态类
/// </summary>
public static int num;
public static void ShowMess()
{
//在静态类里定义静态方法
}
}
}
using System.Security.Cryptography;
namespace InherritanceApplication
{
//继承,类可以互相继承
internal class Program
{
static void Main(string[] args)
{
Rectangle r1 = new Rectangle(10,20);
//r1.setLength(10);
//r1.setWidth(20);
Console.WriteLine(r1.getArea());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InherritanceApplication
{/// <summary>
/// 二维图形
/// </summary>
internal class Shape
{
protected double length;
protected double width;
public void setLength(double length)
{
this.length = length;
}
public void setWidth(double width)
{
this.width = width;
}
public double getArea()
{
return length * width;
}
public Shape(double length, double width)
{
this.length = length;
this.width = width;
}
}
class Rectangle:Shape//继承
{
private double angle = 90;
public Rectangle(double length,double width,double angle):base(length,width)
{
this.angle = angle;
}
public double getArea()
{
return length * width;
}
}
class Triangle : Shape//继承
{
public Triangle(double length, double width):base(length, width)
{
}
public double getArea()
{
return length * width;
}
}
}