运行
环境变量的配置:在环境变量Path中加到JDK下bin的路径
在控制台中 javac 文件名.java编译 文件名.class 文件
再用 java 文件名来执行程序
变量
Java对大小写敏感。
Java字符使用的是Unicode字符集。
类型转换
.ToString() //把int转化为string
integer.parseInit(strings) //把string转化为int
类
class Person
{
String name;
int age;
Person(){
}
void shout()
{
System.out.println("oh!,I'm "+age);
}
}
实例
类名 对象名=new 类名();
继承
在Java当中只支持单继承,不支持多继承
一个类只能有一个父类
class Student extends Person{
void shout(){
super.shout(); //用super关键字来引用父类的函数
System.out.println("I'm a student");
}
}
抽象类和抽象函数
abstract class Person()
{
String name;
int age;
void introduce(){
System.out.println("i'm "+name);
}
abstract void eat(); //抽象函数,有抽象函数的类必须被定为抽象类
}
一个类只能有一个父类
class Student extends Person{
void shout(){
super.shout(); //用super关键字来引用父类的函数
System.out.println("I'm a student");
}
}
抽象类和抽象函数
abstract class Person()
{
String name;
int age;
void introduce(){
System.out.println("i'm "+name);
}
abstract void eat(); //抽象函数,有抽象函数的类必须被定为抽象类
}
运行
环境变量的配置:在环境变量Path中加到JDK下bin的路径
在控制台中 javac 文件名.java编译 文件名.class 文件
再用 java 文件名来执行程序
变量
Java对大小写敏感。
Java字符使用的是Unicode字符集。
final关键字
final 成员变量表示常量
final类不能被继承,final类中的方法也是final的
final成员方法不能够被覆盖
类
class Person
{
static{
//静态代码块,主要是为静态变量设初值,开发中少用
}
String name;
int age;
Person(String name){
this.name=name;
}
Person(String name,int age){
this(name); //this可以调用别的构造函数,以省去重复,这种用法的this必须放在构造函数第一行上
this.age=age;
}
void shout()
{
System.out.println("oh!,I'm "+age);
}
}
实例
类名 对象名=new 类名();
继承
在Java当中只支持单继承,不支持多继承
一个类只能有一个父类
class Student extends Person{
void shout()
{
super.shout(); //用super关键字来引用父类的函数
System.out.println("I'm a student");
}
}
抽象类和抽象函数
abstract class Person()
{
String name;
int age;
void introduce(){
System.out.println("i'm "+name);
}
abstract void eat(); //抽象函数,有抽象函数的类必须被定为抽象类
}
子类在继承抽象类时必须重写父类中的抽象函数
软件包
添加
pakage zxcpakage;
编译时javac -d . testPakage.java // -d是根据包名来生成文件夹 后面的参数是生成文件夹的位置 ' . ' 表示当前目录
编译成功后会在当前目录中出现一个zxcpakage文件夹,里面有testPakage.class文件
但是不能直接java testPakage来执行,也不能进入zxcpakage文件夹后来执行,只能通过java zxcpakage.testPakage来执行
包名的命名规则:1.包名全部小写 2.包名一般是域名反过来写,像com.sina 每个点会多生成一层目录
包的引用:
import org.marsdroid.Person; import org.marsdroid.*;
访问权限
public(其他软件包也可以调用) > protected (只能自己包里或者包里外继承它的子类可以调用)>default(包内) >private
接口
接口 interface
其中的方法都是public权限的抽象方法
一个类可以实现(implements)多个接口
一个类可以继承(extends)多个接口
注:由于接口中的方法已经全都是public了,之后实现接口的对象中重写的方法也一定是public
由于接口中的方法全部是抽象的,所以继承的接口中的函数全部都要实现一遍
在实现接口的类中不向上转换也可以
异常处理
把可能出错的代码放在try(){ }块中
把出异常后执行的代码放在catch(Exception e){ e.printStackTrace(); }
把无论是否出异常都将执行的代码块放在finally{ }块中
声明异常:
public void setAge() throws Exception() //声明异常
IO流
InputStream:
int read(byte []b,int offset,int length); //第一个参数是容器,读的内容放在哪,第二个参数是偏移量,从容器第几位开始放
//第三个是一次读几位
//所以一般第二个参数是0,从头开始放,第三个参数是byte的位数
OutputStream:
int write(byte []b,int offset,int length);
字节流IO
首先,导入JAVA中的IO包
import java.io.*;
class Test{
public static void main(){
FileInputStream ifs=null; //注意是FileInputStream,不是InputFileStream
FileOutputStream ofs=null;
try{
ifs=new FileInputStream("from.txt");
byte[] buffer=new byte[100]; //byte数组
int count=ifs.read(buffer,0,buffer.length); //read方法返回取了多少个字节的值,当没读到数据时候返回-1
/*String s=new String(buffer);
System.out.println(s); */
ofs=new FileOutputStream("to.txt");
ofs.write(buffer,0,count);
}
catch(Exception e){
System.out.println(e);
}
finally{ //关闭IO流
try{
fls.close();
fos.close();
{
catch(Exception e){
System.out.println(e);
}
}
}
}
字符流IO
impotr java.io.*;
class TestChar{
public static void main(String args[]){
try{
FileReader fr=null;
FileWriter fw=null;
char[] buffer=new char[100];
fr=new FileReader("from.txt");
fw=new FileWriter("to.txt");
int temp=fr.read(buffer,0,buffer.length);
fw.write(buffer,0,temp);
}
catch(Exception e){
System.out.println(e);
}
finally{
try{//由于关闭流会抛出异常,所以需进行try catch
fr.close();
fw.close(); //字符流IO,必须把FIleWriter关闭之后才会进行写文件
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
处理流IO
import java.io.*;
class Test{
public static void main(String args[]){
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader("from.txt");
br=new BufferedReader(fr); //这里声明bufferedReader必须有一个FileReader作为参数
while(true){
String line=br.readLine();
if(line==null)
break;
System.out.println(line);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
}
}
}
文件操作
File dir=new File(PATH+dirName); //以路径为参数
dir.mkdir(); //创建路径
File file=new File(PATH+fileName); //路径名
file.createNewFile(); //创建文件
file.exists(); //文件是否已经存在,返回bool
内部类
class A{
int i;
class B{ //编译后会生成一个A.class和一个A$B.class
int j;
public void dosomething()
{
System.out.println("dosomething!");
}
}
}
class Test{
public static void main(String args[]){
A a=new A();
A.B b= a.new B();
b.dosomething();
}
}
内部匿名类
interface A{
public void dosomething();
}
class Test{
public static void main(String args[]){
A a=new A(){
public void dosomething(){
System.out.println("内部匿名类");
}
};
a.dosomething();
}
}
多线程
第一种:
class FirstThread extends Thread{ //继承自Thread类
public void run(){ //重写run方法
for(int i=0;i<100;i++)
System.out.println("FirstThread--->"+i);
}
}
class Test{
public static void main(){
FirstThread ft=new FirstThread;
ft.start();
for(int i=0;i<100;i++)
System.out.println("mainThread-->"+i);
}
}
第二种
class RunnableImp implements Runnable{ //继承了Runnable接口
public void run(){
for(int i=0;i<100;i++){
System.out.println("Runnable--->"+i);
if(i==50){
try{
Thread.sleep(2000); //这个进程休眠2秒,但Thread.sleep会抛出异常,所以需要try catch
}
catch(Exception e){
System.out.println(e);
}
}
}
}
}
class Test{
public static void main(String args[]){
RunnableImp ri=new RunnableImp();
Thread t=new Thread(ri); //不同的声明方法
t.start();
}
}
}
Thread.currentThread().getName()
Thread.currentThread().getId()
线程中断
Thread.sleep(2000); //睡眠
Thread.yield(); //暂时让出CPU,再一起抢
t.setPriority(Thread.MAX_PRIORITY);//设置优先级,最小值为1,最大值为10
t.getPriority()输出优先级
线程控制
同步线程syncronized(this); 同步锁