c#入门问题解决(二)

9.new 修饰符是起什么作用?

答:

new 修饰符与 new 操作符是两个概念

new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员。而new 操作符用于实例化一个类型

new 修饰符只能用于继承类,一般用于弥补基类设计的不足

new 修饰符和 override 修饰符不可同时用在一个成员上,因为这两个修饰符在含义上互相排斥

示例:
None.gif using  System; 
None.gif
using  System.Collections.Generic; 
None.gif
using  System.Text; 
None.gif  
None.gif
namespace  Example09 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
class BaseClass 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
//基类设计者声明了一个PI的公共变量,方便进行运算 
InBlock.gif
        public static double PI = 3.1415
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
class DervieClass : BaseClass 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
//继承类发现该变量的值不能满足运算精度,于是可以通过new修饰符显示隐藏基类中的声明 
InBlock.gif
        public new static double PI = 3.1415926
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
class Program 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            Console.WriteLine(BaseClass.PI); 
InBlock.gif            Console.WriteLine(DervieClass.PI); 
InBlock.gif  
InBlock.gif            Console.ReadLine(); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 
None.gif

结果:
3.1415
3.1415926 


10.this 关键字的含义?

答:

this 是一个保留字,仅限于构造函数和方法成员中使用

在类的构造函数中出现表示对正在构造的对象本身的引用,在类的方法中出现表示对调用该方法的对象的引用,在结构的构造上函数中出现表示对正在构造的结构的引用,在结构的方法中出现表示对调用该方法的结果的引用

this 保留字不能用于静态成员的实现里,因为这时对象或结构并未实例化

在 C# 系统中,this 实际上是一个常量,所以不能使用 this++ 这样的运算

this 保留字一般用于限定同名的隐藏成员、将对象本身做为参数、声明索引访问器、判断传入参数的对象是否为本身

示例:
None.gif using  System; 
None.gif
using  System.Collections.Generic; 
None.gif
using  System.Text; 
None.gif  
None.gif
namespace  Example10 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
class Class1 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
private double c; 
InBlock.gif        
private string value; 
InBlock.gif  
InBlock.gif        
public double C 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
return c; 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public Class1(double c) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//限定同名的隐藏成员 
InBlock.gif
            this.c = c; 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public Class1(Class1 value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//用对象本身实例化自己没有意义 
InBlock.gif
            if (this != value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                c 
= value.C; 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public override string ToString() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//将对象本身做为参数 
InBlock.gif
            return string.Format("{0} Celsius = {1} Fahrenheit", c, UnitTransClass.C2F(this)); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif  
InBlock.gif        
//由于好奇,在这做了一个效率测试,想看看到底哪种方式访问成员变量更快,结论:区别不大。。。 
InBlock.gif
        public string Test1() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
long vTickCount = Environment.TickCount; 
InBlock.gif            
for (int i = 0; i < 10000000; i++
InBlock.gif                
this.value = i.ToString(); 
InBlock.gif            
return string.Format("Have this.: {0} MSEL", Environment.TickCount - vTickCount); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public string Test2() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
long vTickCount = Environment.TickCount; 
InBlock.gif            
for (int i = 0; i < 10000000; i++
InBlock.gif                value 
= i.ToString(); 
InBlock.gif            
return string.Format("Don't have this.: {0} MSEL", Environment.TickCount - vTickCount); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
class UnitTransClass 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
public static double C2F(Class1 value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//摄氏到华氏的转换公式 
InBlock.gif
            return 1.8 * value.C + 32
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
class Program 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            Class1 tmpObj 
= new Class1(37.5); 
InBlock.gif  
InBlock.gif            Console.WriteLine(tmpObj); 
InBlock.gif  
InBlock.gif            Console.WriteLine(tmpObj.Test1()); 
InBlock.gif            Console.WriteLine(tmpObj.Test2()); 
InBlock.gif  
InBlock.gif            Console.ReadLine(); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 
None.gif

结果:
37.5 Celsius = 99.5 Fahrenheit
Have this.: 4375 MSEL
Don't have this.: 4406 MSEL 


11.可以使用抽象函数重写基类中的虚函数吗?

答:

可以,但需使用 new 修饰符显式声明,表示隐藏了基类中该函数的实现

示例: 
None.gif   class  BaseClass 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
public virtual void F() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            Console.WriteLine(
"BaseClass.F"); 
ExpandedSubBlockEnd.gif        }
 
ExpandedBlockEnd.gif    }
 
None.gif    
abstract   class   DeriveClass : BaseClass 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
public new abstract void F(); 
ExpandedBlockEnd.gif    }
 
None.gif
None.gif

   12.密封类可以有虚函数吗?

答:

可以,基类中的虚函数将隐式的转化为非虚函数,但密封类本身不能再增加新的虚函数

示例: 
None.gif class  BaseClass 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
public virtual void F() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            Console.WriteLine(
"BaseClass.F"); 
ExpandedSubBlockEnd.gif        }
 
ExpandedBlockEnd.gif    }
 
None.gif    
sealed   class  DeriveClass : BaseClass 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
//基类中的虚函数F被隐式的转化为非虚函数 
InBlock.gif  
InBlock.gif        
//密封类中不能再声明新的虚函数G 
InBlock.gif        
//public virtual void G() 
InBlock.gif        
//
InBlock.gif        
//    Console.WriteLine("DeriveClass.G"); 
InBlock.gif        
//
ExpandedBlockEnd.gif
    }
 
None.gif
None.gif

    13.如果基类中的虚属性只有一个属性访问器,那么继承类重写该属性后可以有几个属性访问器?如果基类中有 get 和 set 两个呢?

答:

如果基类中的虚属性只有一个属性访问器,那么继承类重写该属性后也应只有一个。如果基类中有 get 和 set 两个属性访问器,那么继承类中可以只有一个也可以同时有两个属性访问器


14.abstract 可以和 virtual 一起使用吗?可以和 override 一起使用吗?

答:

abstract 修饰符不可以和 static、virtual 和 override 修饰符一起使用


15.接口可以包含哪些成员?

答:

接口可以包含属性、方法、索引指示器和事件,但不能包含常量、域、操作符、构造函数和析构函数,而且也不能包含任何静态成员

 

16.类和结构的区别?

答:
类:

类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存

类有构造和析构函数

类可以继承和被继承

结构:

结构是值类型在栈上分配(虽然栈的访问速度比较堆要快,但栈的资源有限放),结构的赋值将分配产生一个新的对象。

结构没有构造函数,但可以添加。结构没有析构函数

结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口

 

示例:

根据以上比较,我们可以得出一些轻量级的对象最好使用结构,但数据量大或有复杂处理逻辑对象最好使用类。

如:Geoemtry(GIS 里的一个概论,在 OGC 标准里有定义) 最好使用类,而 Geometry 中点的成员最好使用结构
None.gif using  System; 
None.gif
using  System.Collections.Generic; 
None.gif
using  System.Text; 
None.gif  
None.gif
namespace  Example16 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
interface IPoint 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
double X 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
get
InBlock.gif            
set
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
double Y 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
get
InBlock.gif            
set
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
double Z 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
get
InBlock.gif            
set
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
//结构也可以从接口继承 
InBlock.gif
    struct Point: IPoint 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
private double x, y, z; 
InBlock.gif        
//结构也可以增加构造函数 
InBlock.gif
        public Point(double X, double Y, double Z) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.x = X; 
InBlock.gif            
this.y = Y; 
InBlock.gif            
this.z = Z; 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double X 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn x; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ x = value; } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double Y 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn x; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ x = value; } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double Z 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn x; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ x = value; } 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
//在此简化了点状Geometry的设计,实际产品中还包含Project(坐标变换)等复杂操作 
InBlock.gif
    class PointGeometry 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
private Point value; 
InBlock.gif         
InBlock.gif        
public PointGeometry(double X, double Y, double Z) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            value 
= new Point(X, Y, Z); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public PointGeometry(Point value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//结构的赋值将分配新的内存 
InBlock.gif
            this.value = value; 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double X 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn value.X; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.value.X = value; } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double Y 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn value.Y; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.value.Y = value; } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public double Z 
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn value.Z; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.value.Z = value; } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public static PointGeometry operator +(PointGeometry Left, PointGeometry Rigth) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
return new PointGeometry(Left.X + Rigth.X, Left.Y + Rigth.Y, Left.Z + Rigth.Z); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public override string ToString() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
return string.Format("X: {0}, Y: {1}, Z: {2}", value.X, value.Y, value.Z); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
class Program 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            Point tmpPoint 
= new Point(123); 
InBlock.gif  
InBlock.gif            PointGeometry tmpPG1 
= new PointGeometry(tmpPoint); 
InBlock.gif            PointGeometry tmpPG2 
= new PointGeometry(tmpPoint); 
InBlock.gif            tmpPG2.X 
= 4
InBlock.gif            tmpPG2.Y 
= 5
InBlock.gif            tmpPG2.Z 
= 6
InBlock.gif  
InBlock.gif            
//由于结构是值类型,tmpPG1 和 tmpPG2 的坐标并不一样 
InBlock.gif
            Console.WriteLine(tmpPG1); 
InBlock.gif            Console.WriteLine(tmpPG2); 
InBlock.gif  
InBlock.gif            
//由于类是引用类型,对tmpPG1坐标修改后影响到了tmpPG3 
InBlock.gif
            PointGeometry tmpPG3 = tmpPG1; 
InBlock.gif            tmpPG1.X 
= 7
InBlock.gif            tmpPG1.Y 
= 8
InBlock.gif            tmpPG1.Z 
= 9
InBlock.gif            Console.WriteLine(tmpPG1); 
InBlock.gif            Console.WriteLine(tmpPG3); 
InBlock.gif  
InBlock.gif            Console.ReadLine(); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 
None.gif

结果:
X: 1, Y: 2, Z: 3
X: 4, Y: 5, Z: 6
X: 7, Y: 8, Z: 9
X: 7, Y: 8, Z: 9 


转载于:https://www.cnblogs.com/lynnwayne/archive/2007/06/08/777134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值