C/C++程序员眼中的Java

JAVA:
一个程序有多个类,但是只有一个类前面允许加public,这个类称为主类,且java源程序的名字必须与主类的名字完全一致,扩展名必须为java,主类中含有main方法,是整个程序的入口。一个java文件中可以有多个类,但是只有一个主类。


数组的定义:
int [] a1 = new int[10];
int a2[] = new int[10];
a.length;
多维数组:
int a[][] = int[2][];//一维的长度可以不指定,但是必须声明二维的长度。
a[0] = new int[2];
a[1] = new int[4];
a[0].length == 2 true;
a[1].length == 4 true;

代码示例(输入输出,类的调用):

import java.io.*; class findsearch { int binarySearch( int arr[], int key) { int low = 0; int high = arr.length -1; int mid = ( low + high ) / 2; while( low <= high && arr[mid] != key) { if( arr[mid] < key ) low = mid + 1; else high = mid - 1; mid = ( low + high ) / 2; } if( low > high ) mid = -1; return mid; } } public class find { public static void main( String[] args) throws IOException { BufferedReader keyin = new BufferedReader( new InputStreamReader( System.in)); int i, k, key, mid; String c1; int arr[] = {2, 4, 7, 9, 18, 25, 34, 56, 68, 89}; System.out.println("原始数据为:"); for( i = 0; i < arr.length; i++) System.out.print(" " + arr[i] ); System.out.println("\n"); System.out.println("输入要查找数:"); c1=keyin.readLine(); key = Integer.parseInt( c1 ); findsearch p1 = new findsearch(); mid = p1.binarySearch( arr, key); if( mid == -1 ) System.out.println("对不起,未找到"); else System.out.println("所查找的数在数组中的位置下标是:" + mid); } }


String类和StringBuffer类:
String类的对象是一经创建便不能变动内容的字符串变量,对String类的对象只能进行查找和比较等操作。用StirngBuffered类创建的对象在操作中可以更改字符串的内容,因此也被称为字符串变量。对于StringBuffered类的对象可以做添加、插入、删除等操作。至于这两个类的常用方法,最好的方法就是查看帮助文档。


代码示例(String的用法):

public class Stringfunction { public static void main( String[] args) { String s1 = "THIS IS A TEST STRING!"; String t = s1.concat(" CONCAT"); boolean result1 = s1.equals( "this is a test string!"); boolean result2 = s1.equalsIgnoreCase( "this is a test string!"); String s2 = "THIS IS A TEST STRING!"; String s3 = new String( s1); boolean result3 = s1 == s2; boolean result4 = s1 == s3; System.out.println("s1 = " + s1 ); System.out.println("t = " + t ); System.out.println("result1 = " + result1); System.out.println("result2 = " + result2); System.out.println("result3 = " + result3); System.out.println("result4 = " + result4); } }


对象和类:
定义类时,需要指出三方面:类的标识,属性,方法。
若没有设置访问控制符,则说明该类,属性,或方法具有默认的访问控制权限,这样的类属性和方法只能被同一个包中的类访问和引用。
final可以用来修饰类、属性和方法。分别表示类不能被继承,属性不能被赋值或隐藏,方法不能被重载和覆盖。
静态属性是一个公共的存储单元,不属于任何一个对象,任何一个类的对象都可以访问它,因此,既可以用"类名.静态属性",也可以用"对象名.静态属性"的形式访问。
静态方法属于整个类,只能处理静态属性,调用方式与静态属性类似。

代码示例(定义类,创建对象,定义静态成员变量和静态成员函数):

public class Student { private String no; private int innerNo; private String name; private char sex; static double pay; static int nextInnerNo; public Student( String n, String str, char ch)//构造函数 { no = n; innerNo = nextInnerNo; nextInnerNo++; name = str; sex = ch; } static//静态初始化器 { pay = 300; nextInnerNo = 1; } public void printStudent() { System.out.println("Student NO:" + no + " innerNo : " + innerNo + " name :" + name + " sex:" + sex + " 助学补助:" + pay); } public static void main( String args[]) { Student s1 = new Student("2011010101", "周涛", '男'); Student s2 = new Student( "2011010103", "小丽", '女'); s1.printStudent(); s2.printStudent(); } }


this and super ,重载:


代码示例(函数的重载,this和super的使用):


class Person { protected String name; protected char sex; } class Student extends Person { String speciality; double score; Student( String name, char sex, String speciality, double score) { //this.name = name; super.name = name; super.sex = sex; this.speciality = speciality; this.score = score; } Student() { this( "anonymout", '男', "unknow", 0.0); } void printStudent() { System.out.println("name: " + name + " sex:" + sex + " major: " + speciality + " grade:" + score ); } } public class ThisSuper { public static void main( String[] args) { Student s1 = new Student( "laimingxing", '男', "Computer", 100.0); Student s2 = new Student( "wangkai", '男', "Computer", 99.0); Student s3 = new Student(); s1.printStudent(); s2.printStudent(); s3.printStudent(); } }

abstract and interface:
用abstract修饰的类被称为抽象类。抽象类是一种没有具体实例的类,它的主要用途是用来描述概念性的内容,这样可以提高开发效率,更好的统一接口。用abstract修饰的方法成为抽象方法,抽象方法没有方法体,抽象方法必须存在有抽象类之中,当然抽象类中可以含有非抽象方法。在抽象类的子类中必须实现抽象类中定义的所有抽象方法。由于抽象类主要是用来派生子类,因此抽象类不能用final修饰,抽象类也不呢更有private的属性和方法。
Java不支持多重继承,即一个类只能有一个直接父类。这使得java程序比较简单,便于操作。但是有时候我们还是希望使用多重继承,则可以通过一个类实现多个接口的方法。接口的修饰符与类的修饰符相同,接口之间可以通过extends实现继承关系,而且可以是多重继承,即一个接口可以继承多个接口。由于接口中定义的方法都是公共的,抽象的,成员都是静态的,公共的常量,所有修饰符可以省略。


接口的实现:

interface A { int a = 100; void showA(); } interface B { int b = 200; void showB(); } interface C extends A, B { int c = 300; void showC(); } public class TestInterface implements C { public void showA() { System.out.println(" a = " + a ); } public void showB() { System.out.println(" b = " + b ); } public void showC() { System.out.println(" c = " + c ); } public static void main( String args[]) { TestInterface d = new TestInterface(); d.showA(); d.showB(); d.showC(); } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值