//用记事本写代码运行Java程序的步骤:
1,记事本写入内容:
public class Test {
public static void main(String[] args) {
System.out.println(“hello world!”);
} }
2,记事本另存为Test.java文件放在E盘下。
3,命令行:
C:\Users\Administrator>E:
E:>javac Test.java
E:>java Test
hello world!
Java运行分为两个阶段
Cat cat;
//编译阶段:原码变成字节码(静态)cat = new Cat();
//运行阶段:字节码在虚拟机上解释执行(动态)
抽象
实例化
对象
类
Scanner
(类)reader
(使用Scanner类创建的一个对象)= new
(计算机不知道Scanner大小,用new的方法动态创建一个内存大小)Scanner
(保持类名一致)(System.in);
//()用来调用构造方法,构造方法的作业是对成员变量进行初始化,System.in表示从键盘赋值int n=reader.nextInt();
//reader对象调用nextInt方法(函数),读取用户在命令行输入的Int数据类型
\t
制表符\n
换行符System.out.println();
//输出为空且换行System.out.print();
//输出时不换行
shift+Alt+F
格式化
ctrl + shift + enter
全屏写代码
alt + shift + enter
显示或影藏边框
Ctrl+/
批量注释
1字节 (byte)= 8 位(bit);2字节 (byte)= 1字符;
例:直接使用float a = 3.14会报错,因为3.14的默认数据是double. float a = 3.14F;
//第一种方法:在数字后面加一个F,告诉编译器这是一个float数 float b = (float) 3.14;
//第二种方法:在数字前面加上 (float)强制类型转换
int a;
System.out.println("a");
输出 aSystem.out.println('a');
输出aSystem.out.println('adf');
//报错,单引号里面只能放单个字符System.out.println('a'+1);
输出98System.out.println("a"+1);
输出a1
int a = 1;
System.out.println(a+1);
//结果为2System.out.println(" " + a + 1);
//结果为11
int a =4;
int b1=a>>1;
//右移除2int b2=a<<1;
//左移乘2System.out.println(b1);
//输出2System.out.println(b2);
//输出8
封装
解决代码安全问题 (封装的实体=数据+方法)
Private: 类可见性
默认:包可见性
Protected:同一个包中的类访问,同一个项目中不同包中的子类访问
Public:可以被同一个项目中所有的类访问,项目可见性
类的封装性:只能通过类本身定义的方法来对该类所实例化的对象进行数据的访问和处理
public class Person{
private int age;//将age的属性设为私有,只有本类可以访问
public int getAge() {
return age;
}
public void setAge(int age){
this.age = age;//对每个值属性提供对外的公共方法访问,也就是创建一对赋取值方法,用于对私有属性的访问
}
}
继承
解决代码复用问题 (使用关键词extends)
public class Father {
private String name ;
private int age ;
分别插入Getter方法、Setter方法、构造函数、ToString()方法
}
public class Son extends Father{
插入构造函数(点灯泡可实现)
}
public class Test5_20 {
public static void main(String[] args) {
Son s1 = new Son("赵四",38);
System.out.println(s1);
}
}
//输出:name = 赵四, age = 38
多态
解决代码扩展问题(包括功能的额外扩展)
Java引用变量有两个类型:
(1)编译时类型。//由声明该变量时使用的类型决定
(2)运行时类型。//运行时的类型由实际赋给该变量的对象决定
如果编译时类型和运行时类型不一致,就会出现所谓的多态。
成员的隐藏和重写
对象的上转型对象
抽象类
接口
方法重载
public class Animal {
public void getVoice(){
System.out.println("动物的声音!");
}
}
public class Cat extends Animal{
@Override
public void getVoice(){
System.out.println("喵喵!");
}
}
public class Dog extends Animal{
@Override
public void getVoice(){
System.out.println("汪汪!");
}
}
public class Test5_20 {
public static void main(String[] args) {
Cat c = new Cat();
Dog d = new Dog();
tune(c);
tune(d);
}
public static void tune(Dog i){
i.getVoice();
}
public static void tune(Cat i){
i.getVoice();
}
}
上转型对象
A a = new B();
//A是B的父类
对象的上转型对象
继承或影藏的变量
继承或重写的方法
对象
public class A {
public int x =10;
@Override
public void print(){
System.out.println("this is A! x = " + x);
}
}
public class SubB extends A{
public int x =30;
@Override
public void print(){
System.out.println("this is B! x = " + x);
?//super.print();
}
}
public class Test5_21 {
public static void main(String[] args) {
A a1 = new SubB();
System.out.println(a1.x);//上转型对象变量使用父类的
a1.print();//上转型对象方法使用子类的
}
}
//注意:(由于子类不继承父类的构造方法,因此子类在其构造方法中需使用 super 关键字来调用父类的构造方法。)
(1)子类SUbB中不加super.print();输出为:
10
this is B! x = 30
(2)子类SUbB中加上super.print();输出为:
10
this is B! x = 30
this is A! x = 10
接口回调
//创建接口A
interface A {
void AList();//声明方法Alist
}
//创建子类B并实现接口A
public class B implements A{
@Override
public void AList() {
System.out.println("521");
}
}
public class Test5_21 {
public static void main(String[] args) {
A a;//声明接口变量
a = new B();//实例化,在接口变量中存放对象的引用
a.AList();//接口回调(接口A中已声明的方法Alist,在子类B中重写)
}
}
内部类
public class Test5_20 {
private int x = 1;
private void print(){
System.out.println(x);
}
class B{
public void print(){
print();
}
}
}
内部类中可以访问到外部类的所有成员,包括private修饰的。
异常类
try{
//代码区
}catch(Exception e){
//异常处理
}
public class A {
public int x =10;
public int getX() {
return x;
}
//自定义异常
public void setX(int x) throws AgeInvalidException{
if(x < 12 || x > 30) {
throw new AgeInvalidException();
}
this.x = x;
}
}
public class AgeInvalidException extends Exception{
public AgeInvalidException() {
super("年龄不合法!");
}
}
public class Test5_21 {
public static void main(String[] args) {
try {
A a1 = new A();
a1.setX(32);
} catch (AgeInvalidException ex) {
System.out.println(ex.toString());
}
}
}
//输出:test5_21.AgeInvalidException: 年龄不合法!
匿名类
(匿名类一定是一个内部类)
jButton(new XX) {
...
});
将上面的匿名类改写为正常类
class A implements XX {
...
}
A a1 = new A();
jButton(a1);
日期
public class Test5_21 {
public static void main(String[] args) {
Date d1 = new Date(1000);//显示计算机起始时间后1000毫秒(1s)的时间
System.out.println(d1);
//输出:Thu Jan 01 08:00:01 CST 1970
java.sql.Date sqlD1 = new java.sql.Date(d1.getTime());//与数据库进行匹配
System.out.println(sqlD1);
//输出:1970-01-01
}
}
public class Test5_21 {
public static void main(String[] args) {
long d2 = System.currentTimeMillis();
System.out.println(d2);//当前时间毫秒
//输出:1590023256563
java.sql.Date sqlD1 = new java.sql.Date(d2);//与数据库进行匹配
System.out.println(sqlD1);//当前时间
//输出:2020-05-21
java.sql.Time sqlT = new java.sql.Time(d2);
System.out.println(sqlT);
//输出:09:11:00
java.sql.Timestamp ts = new java.sql.Timestamp(d2);//。时间戳(年月日+时间)
System.out.println(ts);
//输出:2020-05-21 09:11:51.618
}
}
对日期进行格式化(SimpleDateFormat:具体的实现类)
public class Test5_21 {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yy年MM月");
String str = df.format(new Date());
System.out.println(str);
}
}
//输出:20年05月
日历类Calendar
public class Test5_21 {
public static void main(String[] args) {
Calendar can = Calendar.getInstance();
System.out.println(can.get(Calendar.YEAR));
}
}
//输出:2020
对字符串进行格式化
public class Test5_21 {
public static void main(String[] args) {
double x = 0.12;
System.out.println(String.format("%f%%", x * 10 * 10));
}
}
//输出:12.000000%
//将6输出为五位数的形式,在前面补0
public class Test5_21 {
public static void main(String[] args) {
int y= 6;
System.out.println(String.format("%05d", y));
}
}
//输出:00006
生成一个账单号,规则:yyyyMMdd00000 年月日+流水号(五位数)
public class Bill {
private static int No;
static {
No = 0;//只运行一次
}
private String sBillNo;
public Bill() {
++No;
}
public String getsBillNo() {
GenerateNoA();
return sBillNo;
}
public void GenerateNoA() {
Calendar c1 = Calendar.getInstance();
sBillNo = c1.get(Calendar.YEAR) + "" + String.format("%02d", c1.get(Calendar.MONTH) + 1) + "" + c1.get(Calendar.DATE) + "" + String.format("%05d", No);//""中间不加空格,它存在的意思把内容转化为字符串
}
public void GenerateNoB() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
sBillNo = df.format(new Date()) + String.format("%05d", No);
}
}
public class Test5_20 {
public static void main(String[] args) {
Bill bill= new Bill();
System.out.println(bill.getsBillNo());
Bill billt = new Bill();
System.out.println(billt.getsBillNo());
}
}
//输出:
2020052100001
2020052100002
生成[-23 50]的随机数<随机数的公式:(int)(Math.random()*(大数-小数)) + 小数>
public class Test5_21 {
public static void main(String[] args) {
int md = (int)(Math.random()*73)-23;//(50-(-23))=73需要算出来
System.out.println(md);
}
}
将字符串以“,”作为分隔符分割出来
public class Test5_21 {
public static void main(String[] args) {
String str = "Sy.c,llis(),sdf";
String[] s = str.split(",");
for(String ss : s){
System.out.println(ss);
}
}
}
//输出:
Sy.c
llis()
sdf
输出当前日期的前一天
public class Test5_21 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DATE);
d = d -1;
Date date = new Date(y,m,d);
c.set(y,m,d);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
}
}
//输出:2020-05-20
大数
public class Test5_21 {
public static void main(String[] args) {
System.out.println(Fanc(20));
//计算结果的长度
BigInteger bi = Fanc(20);
System.out.println(bi.toString().length());
}
private static BigInteger Fanc(long x) {
BigInteger jc = new BigInteger("1");//初始化jc
for (int i = 2; i <= x; i++) {
BigInteger st = new BigInteger(String.valueOf(i));
jc = jc.multiply(st);
}
return jc;
}
}
//输出:
2432902008176640000
19
排序
(1)<事先定义排序规则>当在指定类(型)的时候,指定排序规则
Collections.Sort(lst);
(2)<临时指定排序规则>运行时指定排序规则
lst.sort(new Comparator(){});
文件的读写
(1)文件字节流:(只能读字节或字母的ASC||值)
FileInputStream(String name);
FileInputStream(File name);
FileInputStream fr = new FileInputStream("D:\\TestFile.txt");//指明文件位置
while((x = fr.read()) != -1) {//读文件;“>-1”用于确认读出文件内的所有内容
//通过点灯泡实现try_catch语句
FileOutputStream fw = new FileOutputStream("D:\\TestFile.txt");
(2)文件字符流:(可以读中文,一次性写入字符串,包括中文)
//写文件
FileOutputStream fos = new FileOutputStream("D:\\TestFile.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject();
//读文件
FileInputStream fio = new FileInputStream ("D:\\TestFile.txt");
ObjectInputStream ois = new ObjectInputStream(fio);
ois.ReadObject();
(3)缓存流(增加读写功能的类)
BufferReader/BufferWriter
(4)对象流
ObjectInputStream/ObjectOutputStream