Java学习
基本数据类型:
byte 1字节
short 2字节
int 4字节
long 8字节
float 4字节
double 8字节
char 2字节
boolean
使用String.format("%.xf",a);方法可以指定小数保留小数点后x位输出,
其中x表示保留小数点后的位数,a为你要输出的数字
eg: System.out.println(String.format("%.2f",3.14159));
浮点型常量默认为 double 型,因此要使用 float 型常量时,必须将后缀
"F" or "f" 添加到常量中,
eg: float f = 1.234f // 正确
使用 double 型常量时,可以将后缀 "D" or "d" 添加到常量中,也可以不添加
eg: double d = 2.345
double g = 3.145d
移位运算
int i = 8 << 32; // i = 8
int i = 8 << 65; // i = 16
# 在进行移位前,Java 系统首先把 要移的位数 与 被移数的位数 求余数,然后移动余数个位数
Double.POSITIVE_INFINITY // 正无穷
Double.NEGATIVE_INFINITY // 负无穷
Double.NaN // Not a number,表示不知道的意思
java.lang.Math类
Math.PI 圆周率常量
Math.E 自然数常量
Math.ceil()
Math.floor()
Math.abs()
Math.max()
Math.min()
Math.sqrt()
Math.random() // 返回 0.0~10.0 的随机数,括号中无任何参数
Math.round() // 返回最接近操作数的整数(四舍五入),其算法是将操作数加上 0.5,再截断
Math.sin()
Math.cos()
Math.tan()
Math.toDegrees() // 返回给定弧度的角度值
eg: Math.toDegrees(Math.PI*0.5) // 返回90.0
枚举类型表达式
enum LightColor(red, yellow, green)
// 声明枚举类型变量 LightColor,其有 3 种选择:red, yellow ,green
public class Enum
{
public static void main(String agrs[])
{
LightColor lc = LightColor.red;
// 声明枚举变量 lc,并设定其初值
switch(lc) // 对 switch 使用枚举类型
{
case red:
System.out.println("It's red");
break;
case yellow:
System.out.println("It's yellow");
break;
case green:
System.out.println("It's green");
break;
}
}
}
标号语句的两个例子
labe11: // 声明标号 "labe11", 标识紧跟的是 for 语句
for(int i = 0; j = 0; i < 10; i++)
{
while(j < 5)
{
j++;
System.out.println("hello");
break labe11; // 退出外层 for 循环
}
}
/*
* 此段代码执行的结果是打印一次 hello 后,执行 "break labe11" 语句,
* 跳出由 "labe11" 标记的外层 for 循环
*/
labe1:
for(int i = 0; i < 10; i++)
{
int j = 0;
while(j < 5)
{
System.out.println("j = " + j);
if(j == 0)
continue labe1;
j++;
}
System.out.println("i = " + i);
}
/*
* 此段代码的执行结果是打印 10 次 j = 0
*/
Java 面向对象编程基础
对于 public static void main(String argv[]) 的解释:
/*
* main() 方法前的修饰符 public 不是必须的,之所以声明为 public 是
* 为了main() 方法可以从任意的一个 Java 运行环境中调用。但 static
* 修饰符是必须的,只有这样才可以使 main() 方法成为程序的入口点,并
* 通过它直接执行应用程序。
* ================================================================
* main() 方法中的参数定义了一个字符串数组 argv, 是用来从命令行接收用户参数的。
* 采用命令行执行 Java 程序的语句由 4 个部分组成:1.命令名;2.命令参数(可选的);
* 3.应用程序的名称(也是源文件中的主类类名);4.用户输入的参数(多个参数之间用空格分隔)
* 若用户输入参数,则从类名后开始,第一个参数存储于字符串数组 argv[0], 第二个参数存
* 储于 argc[1] 中,以此类推,语法格式如下:
* java [命令参数] 类名 [用户参数 argv[0]] [用户参数 argv[1]] ......
*/
eg:
public class HelloWorld
{
public static void main(String argv[])
{
if(argv.length > 0)
{
if(argv.length == 1)
{
if(argv[0].equals("-a"))
System.out.println("Hello");
else if(argv[0].equals("-b"))
System.out.println("World");
else
System.out.println("Wrong Arguments");
}
else if(argv.length == 2)
{
if(argv[0].equals("-a"))
System.out.println("Hello "+argv[1]);
else if(argv[0].equals("-b"))
System.out.println("World "+argv[1]);
else
System.out.println("Wrong Arguments");
}
}
else
System.out.println("No Arguments");
}
}
执行结果:
对象引用型参数的传递
使用对象引用型参数,使用字符串对象和字符数组对象作为传递的参数。使用时会发现,方法调用后并不会改变字符串对象,但是可以改变字符数组对象的内容。
public class ParamEx{
public static void change(String str, char ch[])
{
str = "Changed";
ch[0] = 'C';
}
public static void main(String argv[])
{
String s = new String("World");
char ch[] = {'H','e','l','l','o'};
change(s, ch);
System.out.println("s = " + s); // 输出: World
System.out.println("ch = " ); // 输出: Cello
for(int i = 0; i < ch.length; i++)
System.out.println(ch[i]);
}
}
类型转换:将字符串转换为双精度变量
double radius = Double.valueOf(r).doubleValue();
this 关键字
this关键字主要有三个应用:
(1)this调用本类中的属性,也就是类中的成员变量;
(2)this调用本类中的其他方法;
(3)this调用本类中的其他构造方法,调用时要放在构造方法的首行。
应用一:引用成员变量
public class Student{
String name;
public void setName(String name)
{
this.name = name;
// 将局部变量的值传递给成员变量
}
}
应用二:调用类的构造方法
public class Student{
public Student(){
this("Li Ming");
// 调用类中的含一个形式参数的构造函数
}
public Student(String name){}
// 定义一个带形式参数的构造函数
}
应用三:返回对象的值
如在代码中,可以使用return this,来返回某个类的引用。此时这个this关键字就代表类的名称。如代码在上面student类中,那么代码代表的含义就是return student。可见,这个this关键字除了可以引用变量或者成员方法之外,还可以作为类的返回值。
Java 面向对象编程进阶
1.定义在方法体外的内部类
(1)成员内部类:
new 外部类构造方法().new 内部类构造方法() OR
外部类对象实例.new 内部类构造方法()
(2)对于静态内部类的引用
new 外部类名.内部构造方法()
2.定义在方法体内的内部类
public class InnerClassDemo {
private String str = "World";
public InnerClassDemo()
{
showMessage();
}
public void showMessage()
{
System.out.println("Now you are in method!");
class InnerClass
{
public InnerClass()
{
System.out.println("This is Inner class constructor");
}
public void showMessage()
{
System.out.println("Hello " + str + "!");
/*方法中的内部类不可以访问该方法的局部变量,但可以访问所在外部类的成员变量*/
}
}
/*创建方法体内的内部对象,并调用内部中方法*/
InnerClass i = new InnerClass();
i.showMessage();
System.out.println("Method end!");
}
public static void main(String args[])
{
new InnerClassDemo();
}
}
输出结果:
Now you are in method!
This is Inner class constructor
Hello World!
Method end!
抽象类
抽象类只能继承一个父类
定义和使用抽象类:
abstract class people
{
String name;
int age;
abstract String getName();
abstract int getAge();
}
class Employee extends people
{
private String name;
int age;
public Employee(String name, int age)
{
this.name = name;
this.age = age;
}
/*实现父类中的抽象方法*/
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
继承
子类中继承了父类中非 private 的全部属性和方法
当父类中的一个方法在子类中重新获得定义时,若该方法的方法名、参数、返回类型均不变,只有方法体发生了变化时,就称子类的方法重载了父类的方法
this 关键字的使用
注意:在 static 型静态方法中,不能使用 this 关键字
class Demo
{
private String name;
pulbic Demo(String name)
{ this.name = name; }
public void show()
{
String str = this.getName();
System.out.println(str);
}
/*用于获取该类对象的方法*/
public Object getObject()
{ return this; }
}
super 关键字的使用
子类调用父类中的构造方法:super(参数1,参数2,…)
调用非构造方法时,直接用方法名就可以了
注意:(1)super() 语句必须放在子类构造方法中的第一行;
(2)不能同时调用 this() 和 super() ,因为这两条语句都必须放在子类构造方法中的第一行
接口 interface
接口的声明:
<访问限制修饰符> [abstract] interface <接口名>
不能用 final 来修饰接口,因为 abstract 代表抽象,final 代表最终(很具体),它们是矛盾的
接口不能设计其父接口中的方法,接口不能实现别的接口
声明举例:
public abstract interface Flyer
{ }
pulbic interface Sailer
{ }
接口也可以继承:
interface Flyer{ }
interface JetFlyer extends Flyer{ }
接口的多重继承:
interface Flyer{ }
interface Jet{ }
interface JetFlyer extends Flyer, Jet
{ }
成员变量在接口中的使用
interface IFEx
{
int intConst1 = 12;
public static final int intConst2 = 13;
}
public class MemInterface
{ /*上面一行如果加上 implements IFEx,下面就可以直接调用 intConst1 了*/
public static void main(String args[])
{
IFEx.intConst1 = IFEx.intConst1 + 1;
/*报错:无法为最终变量 intConst1 指定值*/
System.out.println(IFEx.intConst1); /*输出 12*/
System.out.println(IFEx.intConst1); /*输出 13*/
}
}
方法在接口中的使用
interface Flyer
{
void fly();
/*二者等效*/
public abstract void fly();
}
实现接口
interface Developer{ }
interface Lecture{ }
class Person implements Developer,Lecture
{ }
接口的方法实现
interface Flyer
{
public void fly();
}
interface Sailer
{
public void sail();
}
abstract class AirPlane implements Flyer
{ /*因为该类是抽象的,所以不用实现 Flyer 中的方法*/
public abstract void doAirPlaneThing()
{ }
}
class SeaPlane extends AirPlane implements Sailer
{
/*实现接口和抽象类中的方法*/
pulbic void fly()
{ System.out.println("fly"); }
pulbic void sail()
{ System.out.println("sail"); }
public void doAirPlaneThing()
{ System.out.println("sail and fly"); }
}
接口引用
interface Developer{ }
interface JavaDeveloper extends Developer{ }
interface Lecture{ }
class Person implements JavaDeveloper{ }
/*Person 也间接实现了 Developer 接口*/
public class RefInterface
{
public static void main(String args[])
{
JavaDeveloper jd = new Person();
Developer d = jd;
/*子接口引用可以直接赋给父接口引用,反之不可以*/
jd = (JavaDeveloper)d;
/*父接口引用赋给子接口引用,需要强制类型转换*/
Lecture l = (Lecture)d;
/*无关系的两个接口引用赋值也要强制类型转换,编译可以通过*/
/*但是,因为 JavaDeveloper 接口引用 d 所指向的对象没有直接或间接实现 Lecture接口,所以运行时会报错*/
}
}
类引用和接口引用之间的赋值
interface IFEx1{ }
interface IFEx2{ }
class CEx implements IFEx1
{ }
public class ClassRefInter
{
public static void main(String args[])
{
CEx = new Cex();
IFEx ifex1 = c;
c = (CEx)ifex1;
/*以上两步都是正确的*/
IFEx ifex2 = c;
String s = (String)ifex1;
/*以上两步都是错误的*/
}
}
接口中方法无法使用的修饰符:
public 对应的 private、protected
abstract 对应的 final
instanceof 的使用
instanceof的功能是检查引用指向的对象是否可以看作指定的类型
interface IFather { }
interface ISon extends IFather { }
class Father { }
class Son extends Father implements ISon { }
public class InstanceEx
{
public static void main(String args[])
{
Son s = new Son();
if(s instanceof Son)
System.out.println("s 指向的对象可以看作 Son 类型");
if(s instanceof Father)
System.out.println("s 指向的对象可以看作 Father 类型");
if(s instanceof IFather)
System.out.println("s 指向的对象可以看作 IFather 类型");
if(s instanceof ISon)
System.out.println("s 指向的对象可以看作 ISon 类型");
}
}
输出结果:以上判断语句全部返回 True,所有信息都打印了
若进行测试的引用为 null 值,其结果总是返回 False
数组引用的使用:
数组也是对象,其引用也可以用 instanceof 进行测试,例如:
Son[] sonArray = new Son[12];
if(sonArray instanceof Son[]){ }
if(sonArray instanceof Father[]){ } /*子类型的数组可以看作是父类型的数组*/