C# VS JAVA 差异 (未完待续)
1. 静态构造函数
C#中有静态构造函数, Java中没有静态构造函数。其实Java中有一个类似静态构造函数的东东,称作静态初始化,或者静态代码块,可以通过这样的代码实现相同的功能:
但是Java中静态代码块和C#静态构造函数还是不一样的。C#中静态构造函数在其他静态成员初始化后再执行,而java中静态代码块和其他静态成员谁在先谁就先执行。
class Parent{
public static StaticVariable staticVariable = new StaticVariable("Parent - Static Variable1");
public StaticVariable inStaticVariable = new StaticVariable("Parent - Instant Variable1");
static
{
System.out.println("Parent - Static block/constructor");
System.out.println(staticVariable == null);
//System.out.println(staticVariable2 == null); not working because staticVariable2 is not defined
staticVariable2 = new StaticVariable("Parent - Static Variable2 - Static block");
}
{
System.out.println("Parent - Initializer Block");
}
public static StaticVariable staticVariable2 = new StaticVariable("Parent - Static Variable2");
public StaticVariable inStaticVariable2 = new StaticVariable("Parent - Instant Variable2");
public Parent()
{
System.out.println("Parent - Instance Constructor");
}
}
View Java Code
class StaticDemo
{
static int i = ; static StaticDemo()
{
Console.WriteLine(i);
Console.WriteLine(j);
} public static void Execute()
{
} static int j = ;
}
View C# Code
2常量
Java中声明(实例/类)常量使用关键词(final/static final)。C#中声明(实例/类)常量使用关键词(readonly/const 或者 readonly static).
C#中必须使用类名去访问类层级的变量。Java中可以使用实例去访问类层级的变量,但是编译时会有警告。
Java Code and C# Code
class ParentDef{
public static final String STATICVALUE_STRING="Parent Static Variable";
public String valueString="Parent Instant Variable";
}
class ChildRef extends ParentDef{
public static final String STATICVALUE_STRING="Child Static Variable";
public String valueString="Child Instant Variable";
}
public class BasicDemo {
public static void main(String[] args) {
//Child child = new Child();
ParentDef pdf = new ParentDef();
ParentDef pcdf = new ChildRef();
ChildRef cdf = new ChildRef();
System.out.println("V1");
System.out.println(pdf.STATICVALUE_STRING);
System.out.println(pdf.valueString);
System.out.println("V2");
System.out.println(pcdf.STATICVALUE_STRING);
System.out.println(pcdf.valueString);
System.out.println("V3");
System.out.println(cdf.STATICVALUE_STRING);
System.out.println(cdf.valueString);
}
}
class InheritenceDemo
{
public static void Execute()
{
ParentDef pdf = new ParentDef();
ParentDef pc = new ChildDef();
ChildDef cdf = new ChildDef(); Console.WriteLine("V1");
Console.WriteLine(pdf.value); Console.WriteLine("V2");
Console.WriteLine(pc.value); Console.WriteLine("V3");
Console.WriteLine(cdf.value);
Console.WriteLine(cdf.READONLYSTRING); }
} class ParentDef
{
public const string Const_String = "Parent Const Varialbe";
public static string STATICVALUE_STRING = "Parent Static Variable";
public string value = "Parent Instant Variable";
} class ChildDef:ParentDef
{
public readonly string READONLYSTRING="Child readonly variable";
public readonly static string READONLYSTATICSTRING = "Child readonly static variable";
public static string STATICVALUE_STRING = "Child Static Variable";
public string value = "Child Instant Variable"; public ChildDef()
{
READONLYSTRING = "NEW Child readonly variable";
//READONLYSTATICSTRING = "NEW Child readonly static variable"; ERROR as satatic readonly variable can not be reassianged in instant constructor
}
}
3参数传递
C#中有ref关键词用来按引用传递参数。Java则没有,无法真正按引用传递参数。Java总是采用按值调用。方法得到的是所有参数值的一个拷贝,特别的,方法不能修改传递给它的任何参数变量的内容。
(1):“在Java里面参数传递都是按值传递”这句话的意思是:按值传递是传递的值的拷贝,按引用传递其实传递的是引用的地址值,所以统称按值传递。
(2):在Java里面只有基本类型和按照下面这种定义方式的String是按值传递,其它的都是按引用传递。就是直接使用双引号定义字符串方式:String str = “Java”;
C# code
class RefExample
{
static void Method(ref int i)
{
i = ;
}
static void Main()
{
int val = ;
Method(ref val); // val is now 44
}
}
4虚函数
C#中普通成员函数加上virtual关键字就成为虚函数.
Java中其实没有虚函数的概念,它的普通函数就相当于C#的虚函数,动态绑定是Java的默认行为。如果Java中不希望某个函数具有虚函数特性,可以加上final关键字变成非虚函数.
5空接口
在Java和C#中空接口都是合法的。都可以定义空接口。空接口还是很奇怪的存在,个人的理解是空接口仅做标记使用,无其他含义。如果你只需要在运行时区分这些类型,一个更佳的解决方式是使用自定义属性(attribute)。如果你希望在编译时区分这些类型,就只好使用空接口了。 而JDK中定义的很多空接口很多都是前者,个人认为这是一种不良的设计,反观CLR,我们则很难找到一个空接口。 所以.Net的设计在这点上来看跟合理点。
JDK中定义的空接口
java.io.Serializable;
java.lang.Cloneable
java.lang.annotation.Annotation;
java.rmi.Remote;
java.util.RandomAccess;
6Delegate 和 Event
Java欠缺C#中的事件功能,java中没有delegate这种用法。在java中实现事件功能其实就是通过实现EventListener接口实现观察者模式。C#则是通过EventHandler的实例来实现事件的功能。
class EventDemo
{
public static void RunDemo()
{
SMS sms = new SMS();
SmsReceiver r = new SmsReceiver(sms);
sms.SendSms("","Hello world");
}
} public class SMS
{
public EventHandler<SmsEventArgs> SmsEvent = (o, e) => { };
public EventHandler SmsEvent2; protected virtual void OnSmsEvent(SmsEventArgs e)
{
//EventHandler<SmsEventArgs> handler = this.SmsEvent;
//if (handler != null)
//{
// handler(this, e);
//} this.SmsEvent(this, e); } protected virtual void OnSmsEvent2()
{
EventHandler handler = this.SmsEvent2; if (handler != null)
{
handler(this,null);
}
} public void SendSms(string phone, string message)
{
SmsEventArgs e = new SmsEventArgs();
e.Message = message;
e.ToPhone = phone;
OnSmsEvent(e);
}
} public class SmsEventArgs:EventArgs
{
public string ToPhone { get; set; }
public string Message { get; set; }
} public class SmsReceiver
{
public SmsReceiver(SMS sms)
{
sms.SmsEvent += new EventHandler<SmsEventArgs>(sms_SmsEvent);
} void sms_SmsEvent(object sender, SmsEventArgs e)
{
Console.WriteLine(e.ToPhone + ":" + e.Message);
} }
View C# Code
public class AskEvent extends EventObject {
private static final long serialVersionUID = 1L;
private Object Evnetsource;
private String name;
public Object getEvnetsource() {
return Evnetsource;
}
public String getName() {
return name;
}
public AskEvent(Object source,String name) {
super(source);
Evnetsource = source;
this.name = name;
}
}
public interface Listener extends EventListener {
public void listen(AskEvent ae);
}
public class Ask {
private Listener l ;
private List<String> names = new ArrayList<String>();
public void addListener(Listener l){
this.l = l;
}
public void addName(String name){
names.add(name);
}
public void setFlag(boolean flag){
if(flag){
if(names.size()==) System.out.println("Input Name!!!");
for(int i = ;i<names.size();i++){
l.listen(new AskEvent(this,names.get(i)));
}
names.clear();
}
}
}
public class EventTest {
public static void main(String[] args) {
System.out.println("START");
Scanner scan = new Scanner(System.in);
Ask ask = new Ask();
ask.addListener(new Listener(){
public void listen(AskEvent ae) {
if(ae.getName().equals("a")) System.out.println(ae.getName() + "good man");
else System.out.println(ae.getName() + "bad man");
}
});
while(true){
System.out.print("input name:");
final String name = scan.nextLine();
if(name.equals("exit")) break;
if(name.equals("print")) {
ask.setFlag(true);
continue;
}
ask.addName(name);
}
System.out.println("OVER");
}
}
View Java Code
7Exception
Java中Checked Exception是必须要被try_catch捕获,编译器会强制检查的。在C#中是否捕获Exception并不是由编译器强制的。
C# VS JAVA 差异 (未完待续)的更多相关文章
- spark集群搭建(java)未完待续
环境 操作系统:windows10 虚拟机工具:VMware14.1 NUX版本:Centos7.2(64) JDK:1.8(64) 一.安装linux,master(桥接模式上网),slave(na ...
- Java开发中的23+2种设计模式学习个人笔记(未完待续)
注:个人笔记 一.设计模式分三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模 ...
- java泛型基础、子类泛型不能转换成父类泛型--未完待续
参考http://how2j.cn/k/generic/generic-generic/373.html 1.使用泛型的好处:泛型的用法是在容器后面添加<Type>Type可以是类,抽象类 ...
- javascript有用小功能总结(未完待续)
1)javascript让页面标题滚动效果 代码如下: <title>您好,欢迎访问我的博客</title> <script type="text/javasc ...
- MVC丶 (未完待续······)
希望你看了此小随 可以实现自己的MVC框架 也祝所有的程序员身体健康一切安好 ...
- 2017-2-17,c#基础,输入输出,定义变量,变量赋值,int.Parse的基础理解,在本的初学者也能看懂(未完待续)
计算机是死板的固定的,人是活跃的开放的,初学c#第一天给我的感觉就是:用人活跃开放式的思维去与呆萌的计算机沟通,摸清脾气,有利于双方深入合作,这也是今晚的教训,细心,仔细,大胆 c#基础 1.Hell ...
- Hibernate二级缓存(未完待续)
1.Hibernate的cache介绍: Hibernate实现了良好的Cache机制,可以借助Hibernate内部的Cache迅速提高系统的数据读取性能.Hibernate中的Cache可分为两层 ...
- jdbc14 及 jdbc16 共存所带来的问题【未完待续】
在JAVA中JDK版本号与JDBC版本号的一致性十分重要,开发都们经常会忽略了这一点导致非常多不必要的错误. 昨天给客户排查了一个关于EDB在JBoss中使用时关于这方面的问题,希望给大家一点启示. ...
- git安装与使用,未完待续... ...
目录 一.git概念 二.git简史 三.git的安装 四.git结构 五.代码托管中心-本地库和远程库的交互方式 六.初始化本地仓库 七.git常用命令 1.add和commit命令 2.sta ...
随机推荐
- linux 下文件节点索引
最近发现一个奇怪的问题,就是一个pyhton 后台的服务一直打印日志文件,在中间我用vim看日志文件,关闭时习惯性的:wq退出,在此之后日志文件就不输出了. 1 对于这个现象我开始认为是python ...
- SilverLight - Memory Leak
There is a memory leak issue in current silverlight project. It occurs in the search function: the m ...
- Java 在某一个时间点定时执行任务(转载)
java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...
- 安装 composer SSL operation failed with code 1
gavin@webdev:~> curl -sS https://getcomposer.org/installer | php Downloading... Download failed: ...
- [Linux技巧]固定Vmware下CentOS的IP
1. 首先取消使用Vmware动态主机设置服务 [Edit] -> [Virtual Network Editor ...] 打开面板,选中[VMnet8]. 如下,取消对[ Use local ...
- SpringMVC与ajax相关知识练习与存档
参考文章(亲测有效): SpringMVC+ajax返回JSON串 jquery ajax例子返回值详解 AJAX $.ajax({url:路径,date:数据,}) //get请求(指定回调函数,参 ...
- 【Java】XML解析之DOM4J
DOM4J介绍 dom4j是一个简单的开源库,用于处理XML. XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP,使用需要引用dom4j.jar包 ...
- (Python )控制流语句if、for、while
这一节,我们将学习Python的控制流语句,主要包括if.for.while.break.continue 和pass语句 1. If语句 if语句也许是我们最熟悉的语句.其使用方法如下: x=inp ...
- ios php RSA 非对称加密解密 der 和pem生成
ios 使用public_key.der加密 php 使用 private_key.pem解密 openssl req -x509 -out public_key.der -outform der - ...
- tomcat提供文件下载
引用两篇博客:http://blog.csdn.net/yuan882696yan/article/details/26680253 http://www.cnblogs.com/shenliang1 ...