Java 命令行+ Java基础

命令行:
1. "cd.." 返回文件夹上一层。 "dir"显示当前目录文件


2. 先用cd,进入文件所在目录。 javac,为编译;  java  为运行。 "javac *.java"编译所有以 ".java"为结尾的文件。
   cls,清屏。
   
3.将类放置到一个保重,需要使用package“包名”
编译时使用-d参数,改参数的作用是依照包名生成相应的文件夹。
一个类的全名应该是 “包名” + "." + “类名”
package mars; //java代码中设置包名
javac -d. Test.java //用来编译程序
java mars.Test //用来运行程序。


4. 包名的导入
improt + 包名 ; 之后就不用在使用类的时候,在类名前加包名了




java基础:

1.访问权限
1.public 跨包,可修饰类,成员变量,成员函数
2.protected    跨包 继承父类protect方法的子类, 可修饰变量,成员函数
3.default 只能在本包内, 可修饰类,变量,函数
4.private 只能在本包内, 本类中(子类不可)。 可修饰变量,函数

2.如果一个方法中,有与当前类中成员变量同名的局部变量.则方法中访问的均为方法内的局部变量。


3.Dog d = new Dog(); Dog d:创建一个Dog的引用,“d”就是引用,放在栈内存中。  new Dog():创建一个Dog对象,在堆内存中创建空间创建,并调用构造函数. ”=“:将创建的Dog对象赋给给这引用。


4.类:一系列具有相同属性的事物。ex:狗   对象:某一事物中的具体一个。ex:金毛。


5.public static void main(String args []){ }
   
   
6.匿名对象: new Dog().jump() ,因为没有名字,所以只能使用一次。下次会重新创对象。 


7.构造函数: 1.无返回值,必须与类名相同. 2.创建类 3.方便赋初值
public class Person{
//自动生成无参构造函数(仅在类中没有构造函数的时候,自动生成,如果有构造函数则不生成)
Person(){
}
//重写的重载构造函数,(用于给类属性赋初值,比较方便)
Person(String n ,int a){
name = n;
age = a;
}
}
class Test{
public static void main(String args[]){
Person person1 = new Person("zhangsan", 10);
Person person2 = new Person("lisi", 20);
}
}

8. this: 当前类;成员变量;参数赋值给成员变量; 调用本类当中的另一个构造函数;
//参数赋值给成员变量
Person(String name, int age){
this.name = name;
this.age = age;
}
//调用本类当中的另一个构造函数,且必须为本类当中的第一条语句
Person(String name, int age, String address){
this(name,age);
this.address = address;
}

9. 静态变量static int i:(可以理解为类的变量)
1.能直接用类名调用,(也可创建引用来调用)
2.创建N个引用,对其中一个赋值,其他成员变量也跟着一起改变
静态函数static void fun():
 1.能直接用类名调用,(也可创建引用来调用)
2.静态函数,要引用静态成员变量
静态代码块 static{ }
 1.在装载类时自动运行,无需调用,主要用于给静态变量赋初值,运用较少。


10. super: 
1.子类中,如不自己声明(super(),根据里面的参数来判定调用哪个构造函数),自动调用父类无参构造函数(super());
2. "super()"调用父类构造函数,必须在第一行。  "super."调用父类成员函数,可以不再第一行


11. 对象的转型和引用
//一个引用能够调用哪些成员(变量和函数),取决于这个引用的类型(Person)
//一个引用调用的是哪一个方法,  取决于这个引用所指的对象(Student)
Student s = new Studetn();
Person p =s;
12.抽象类 abstract
1.抽象类不能生成对象
2.有抽象函数的类,必须声明为抽象类
3.没有抽象函数的类,也可以被声明成抽象类

13. 接口
1.使用interface定义, implements进行实现(要将接口中的全部抽象方法都实现)
2.接口中的方法全都是抽象方法
3.接口中的方法权限都是public

14. 异常:中断了正常指令流的事件。
System.out.println(1);
int i = 1/0; //0作为除数,报异常 “1” 可以打印出来
System.out.println(2);
1.UnCheckException 异常  ex: int i =1/0 ; 直接报出异常类型,不需要抛错处理,。
2.CheckException 异常 ex: Thread.sleep(1000); 需要捕捉异常,遇到异常指令跳到catch中运行,
之后运行下面代码(如不捕捉异常地方程序不往下运行)需要做抛错处理。
try{
// "finally" ex: 打开文件
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
// "finally" ex: 关闭文件
//可以不写,用作异常出口,不论有没有异常都执行
}

throw 用法:作为虚拟机无法理解的异常补充, 当在主函数中,调用方法给age负值为负数时,则跑出异常信息,打印出"年龄不能为负数"
pubilc void setAge(int age){
if(age < 0){
//runTimeException 属于UnCheckException;
//Exception 属于 CheckException;
RuntimeException e = new RuntimeException("年龄不能为负数");
throw e;
}
}
如果需要捕捉异常,可以:
pubilc void setAge(int age) throws Exception{} //声明异常,在调用的地方再用try,catch进行捕捉
try{
user.setAge(-20);
}
catch(Exception e){
System.out.println(e);
}
或者,直接在 setAge方法里进行异常捕捉。

15. 字节流: 父类:InputStream, OutputStream(抽象的)
主要方法: int read(byte[]b, int off, int len);  void write(byte[]b, int off, int len);
import java.io.*;
public class Test{
public static void main(String args []){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("文件所在路径e:src/from.txt");
fos = new FileOutputStream("文件所在路径e:src/to.txt");
byte [] buffer = new bute[1024];
//读数据时,如果设偏移量off,记得要在长度那里减去off 防止数组越界
while(true){
int temp = fis.read(buffer ,0 , buffer.length); //read的返回值 为读取了多少个字节的数据
if(temp == -1){
break;
}
fos.write(buffer ,0 , temp);
}

//打印输入流中,读到buffer中的数据
// for(i=0,i<buffer.length,i++){
// System.out.println(buffer[i]);
// }

//将buffer中的数据转换成一个字符串  s.trim方法:消除字符串中的首尾空字符
// String s = new String(buffer)
// s = s.trim()
// System.out.println(s);
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
catch(Exception e){
System.out.printly(e);
}
}
}
}


16.字符流: 读写文件时,以字符为基础。
字节输入流:父类 Reader  <---- FileReader
int read(char [] c, int off, int len)
字节输出流: Writer <---- FileWriter
int write(char [] c, int off, int len)


FileReader fis = null;
FileWriter fos = null;

fis = new FileReader("路径");
fos = new FileWriter("路径");


17. 处理流: BufferReader. 按行读取
FileReader fileReader = null;
BufferedReader bufferedReader = null;

fileReader = new FileReader(" 路径");
bufferReader = new BufferReader(fileReader); //处理流
String line = bufferReader.readLine;
System.out.pintln(line);

18.装饰者模式


19.内部类
public class A{
clasee B{
//B中可以使用,但不拥有A的成员变量;
}
}
A  a = new A();
A.B  b = new A().new B(); //或 A.B b = a.new B();


20.匿名内部类
//A 为接口,B以A类型的对象作为参数,调用A类型的方法,A为接口要写一个A类的实现来创建对象
public class B{
public void fun(A a){
System.out.println("B类的fun函数");
a.doSomething();
}
}

b.fun(new A(){
public void doSomething(){
System.out.println("匿名内部类");
}
});

21.线程.创建线程:
1. 定义一个线程类FirstThread,它继承类Thread并重写其中的方法run(), 
方法run()称为线程体,在主函数中 "对象.start" 启动线程。
FirstThread ft = new FirstThread();
ft.start();

2. 提供一个实现接口Runnable的类RunnableImpl作为线程的目标对象,在其中重写run()方法
在初始化一个Thread类或者Thread子类的线程对象时,把目标对象传递给这个线程的实力,由该目标对象提供给线程体
RunnableImpl ri = new RunnableImpl();
Thread t = new Thread(ri);
t.start();
Thread.sleep(); //让线程休眠
Thread.yield(); //让出线程优先级(让别的线程先执行 

线程优先级:  优先级越高,被执行的概率越大,(概率!)
设置优先级为最高:t.setPriority(Thread.MAX_PRIORITY); //最大是10,最小是1
获取当前线程优先级: t.getPriority();

设置线程的名字 t1.setName("线程a");
// 取得当前正在执行线程的 名字
Thread.currentThread().getName()

同步代码块,线程锁 //解决多个线程同时执行,且共用一个数据“i”,会有bug
//(线程运行到代码块中某一行时,被其他线程抢到优先级,又从头运行)
synchronized(this){
System.out.println(Thread.currentThread().getName() + i);
i--;//两个线程共用一个i--,没执行完这句就更换了线程
Thread.yield();
if(i<0){
break;
}
}

22. 数组的静态声明法 :int arr [] = {5,3,5,4,9,2};
数组的动态声明法 :int arr [] = new int[10]; // 不赋值,则是10个0
二维数组: int arr [] [] = {{1,2,3},{4,5,6},{7,8,9}};
  int arr [][] = new int [3][5];

23. 类集: 集合,列表,映射(键值对)
列表
import java.until.List;
import java.until.ArrayList;
public class Test{
public static void main(String args[]){
// " <>" 泛型,里面放什么类型,数组就只能存放什么类型的对象、
// ArryList 获取元素长度(几个元素),用 arrylist.size();
// 移除某个元素 arrylist.remove("元素位置");
ArryList<String> arrList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
for(int i = 0; i < arrList.size() ; i++){
String s = arryList.get(i);
System.out.println(s);
}
}
}

Set/Hashset(集合,不允许重复,无序)
import java.util.Set;
import java.util.HashSet;
public class Test{
public static void main(String args[]){
HashSet<String> hashSet = new HashSet<String>();
//Iterator <-- Collection <-- Set <---HashSet
//HashSet 主要方法: hasNext(); next()
HashSet<String> set = hashSet; //向上转型
set.add("a");
set.add("b");
set.add("c");
set.add("a"); // 添加结果不变,集合不允许重复元素
//set.clear(); 清空集合
//set.remove("a");  移除某个元素
//set.isEmpty(); 判断集合是否为空

Iterator<String> it = set.iterator();
boolen b1 = it.hasNext();
while(b1){
String s = it.next();
System.out.println(s);
}
}
}

Map/HashMap (映射——键值对)
import java.util.Map;
import java.util.HashMap;
public class Test{
public static void main(String args[]){
HashMap<String,String> hashMap = new HashMap<String,String>();
Map<String,String> map = hashMap;

map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
map.put("5","e");
//map.put("1","f"); 后面添加的相同键,值会被覆盖掉

int i =map.size();
String s = map.get("1");
System.out.println(s);

}
}

23. "==" 比较两个引用指向的对内存 是不是同一块儿
" equals " 比较两个对象是否相等(对象类型,对象的成员变量的值) // u1.equals(u2);
//通常要复写equals 
原型: public boolean equals(object boj){
return (this == obj);
}
复写: //基本数据类型用 "=="比, 引用数据类型用"equals"比
public class User{
String  name;
int     age;
public boolean equals(object bbj){
if(this == obj){
return true;
}
// "instanceof "操作符,判断前面的对象,是不是后面的这个类型
boolean b = obj instanceof User;
if(b){
User u = (User)obj;
//这里调用的equals,是jdk中没有复写的原型
if(this.age == u.age && this.name.equals(u.name)){
return true;
}
else{
return false;
}
}
else {
return false;
}
}
}














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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值