110,571
社区成员
发帖
与我相关
我的任务
分享
public class MyParent//父类
{
public void writeLine()
{
MyChildren cc = new MyChildren();
//print(); //--------------------------------这个地方是肯定没法调用的!!!!!为什么????????
//
/*
* 既然Children是Parent的子类,那么应该是先有父亲,再有儿子,对吧?
* 那么父亲要编绎成功了,儿子才能编绎成功,是不?也就是说你先把儿子杀了(删掉该文件),
* 你去编绎父亲,看你能编绎成功不?
*/
cc.print("aa"); //正确。调用子类的print方法,没有任何意义。
//cc.pring(); //出错,为什么?因为(出了自己类的声明范围,在外界(实例此类的对象)就只能发能调用public的方法了)
}
protected void MyParentMethod()
{
}
}
public class MyChildren : MyParent//子类
{
protected void print()
{
MyParentMethod(); //正确,子类继承父类,可以使用任何父类非private方法
writeLine(); //正确,子类继承父类,可以使用任何父类非private方法
}
public void print(string str)
{
MyParent p = new MyParent();
p.writeLine(); //正确。
//p.MyParentMethod() //出错(出了自己类的声明范围,在外界(实例此类的对象)就只能发能调用.
}
}
public class MyTestClass //无关类
{
MyParent p = new MyParent();
public void test()
{
p.writeLine(); //正确
//p.MyParent(); //出错(出了自己类的声明范围,在外界(实例此类的对象)就只能发能调用public的方法了)
}
}
class A
{
protected int i = 10;
}
Main()
{
A a = new A();
int x = a.i; //不可以,我们绕一下:加上B类定义,再用下面一行。
int x = new B().GetI(a);
}
class B : A //这个类专门用于获得i
{
public int GetI(A a)
{
return a.i;
}
}
class A
{
public void foo()
{
B b = this as B;
if (b != null) b.print();
}
}
class B : A
{
public void print() { ... }
}
Main()
{
A a = new B();
a.foo();
}
class Home
{
private int money = 1000;
public void GetMoney(Home anotherHome)
{
Console.WriteLine(this.money); // 可以,this.money 可以简写 money
Console.WriteLine(anotherHome.money); // 不可以
}
}
public class MyTestClass //无关类
{
MyParent p = new MyParent();
public void test()
{
p.writeLine(); //正确
//p.MyParent(); //出错(出了自己类的声明范围,在外界(实例此类的对象)就只能发能调用public的方法了)
}
}