编程核心思想
- 基本框架
- 第一天(继承)
- 第二天(抽象和接口)
- 第四天(多态、DATA、Object、正则)
- 第五天(遍历、Set、斗地主游戏案例)
- 第六天
- 第七天
b站链接:懂不懂我意思明不明白哈哈哈
基本框架
1.先建一个空项目
2.建一个新模块
3.建一个包
4.建一个class类
类中五大成份:
成员变量Field、成员方法Method、构造器Constructor、代码块、内部分
构造器:初始化一个类的对象并返回引用·
格式:修饰符 类名(形参){ }
初始化对象格式:类名 对象名称=new 构造器;
this关键字:代表当前对象的引用
可以用在实例化方法和构造器中
用在方法中,谁调用这个方法,this就代表谁
用在构造器,代表构造器正在初始化的那个对象的引用
知识点:
1:封装
提高安全性
实现代码组件化
成员变量私有:用private修饰
成套的getter+setter方法暴露成员变量的取值和赋值,piblic修饰符公开
2:this关键字
3:static关键字
静态成员变量、实例成员变量
静态方法、实例方法
package com._01;
public class ClassDemo01 {
public static String schoolName="黑马";
private String name;
private int age;
public static void main(String[] args){
System.out.println(ClassDemo01.schoolName);
ClassDemo01 swk=new ClassDemo01();
swk.name="孙悟空";
System.out.println(swk.name);
System.out.println(swk.age);
System.out.println(swk.schoolName);
}
}
4: 继承
5:多态
第一天(继承)
方法访问
package com._01;
public class ClassDemo2 {
private String name;
private int age;
public static void inAddr(){
System.out.println("搞学习");
}
public void eat(){
System.out.println(name+"已经"+age+"岁。在吃好吃的");
}
public static void main(String[] args){
ClassDemo2.inAddr();
inAddr();//在同一个类中访问静态成员可以省略不写
//对象 实例化
ClassDemo2 zbj=new ClassDemo2();
zbj.name="八戒";
zbj.age=1000;
zbj.eat();
zbj.inAddr();//不推荐
}
}
继承
public class dome1 {
public static void main(String[] args){
Cat c=new Cat();
}
}
class Cat extends Animal{
}
class Animal{
public String name;
public void run(){
}
}
子类可以继承父类私有成员,但是不能访问
public class dome1 {
public static void main(String[] args){
Cat c=new Cat();
Cat.test();
System.out.println(Cat.schoolName);
}
}
class Cat extends Animal{
}
class Animal{
public static String schoolName="黑马";
public static void test(){
}
public void run(){
}
}
方法重写
public class dome1 {
public static void main(String[] args){
Cat c=new Cat();
c.run();
}
}
class Cat extends Animal{
@Override //校验注解,写错会报错,安全
public void run(){
System.out.println("跑的贼快");
}
}
class Animal{
public void run(){
System.out.println("动物可以跑步");
}
}
super调用父类被重写
public class dome1 {
public static void main(String[] args){
Cat c=new Cat();
c.go();
}
}
class Cat extends Animal{
@Override //校验注解,写错会报错,安全
public void run(){
System.out.println("跑的贼快");
}
public void go(){
super.run();
run();
}
}
class Animal{
public void run(){
System.out.println("动物可以跑步");
}
}
静态方法和私有方法都不可被重写
继承后构造器的特点
this和super关键字
继承特点
3.一个类可以有多个子类
4.一个类要么继承了object类,要么简接继承了object类,是java中祖宗类
引用类
第二天(抽象和接口)
抽象类定义
public class dome1 {
public static void main(String[] args){
}
}
class Dog extends Animal{
@Override //校验注解,写错会报错,安全
public void run(){
System.out.println("跑的贼快");
}
}
class Cat extends Animal{
@Override //校验注解,写错会报错,安全
public void run(){
System.out.println("跑的贼快");
}
}
abstract class Animal{
public abstract void run();
}
抽象类使用
package Demo1;
public class demo1 {
public static void main(String[] args){
Teacher a=new Teacher();
a.work();
Manage b=new Manage();
b.work();
}
}
class Manage extends Employee{
@Override
public void work(){
System.out.println("管理");
}
}
class Teacher extends Employee{
@Override
public void work(){
System.out.println("讲课");
}
}
abstract class Employee{
public abstract void work();
}
抽象类特征
抽象类存在意义
package Demo1;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
public class demo1{
public static void main(String[] args){
Student lty=new Student();
lty.write();
Teacher t=new Teacher();
t.write();
}
}
class Teacher extends demo2{
@Override
public String writeMain(){
return "\t\tcucjb";
}
}
class Student extends demo2{
@Override
public String writeMain(){
return "\t\tcucjb";
}
}
abstract class demo2 {
private String title="wdbbb1";
private String one="wdbbb1";
private String last="wdbbb1";
public void write(){
System.out.println(title);
System.out.println(one);
System.out.println(writeMain());
System.out.println(last);
}
public abstract String writeMain();
}
接口
接口多实现
package Demo1;
public class demo1{
public static void main(String[] args){
Basketball yao=new Basketball();
yao.rule();
yao.competition();
yao.run();
}
}
class Basketball implements SportMan,Law{
@Override
public void run(){
}
@Override
public void competition(){
}
@Override
public void rule(){
}
}
interface SportMan{
void run();
void competition();
}
interface Law{
void rule();
}
接口与接口的多继承
package Demo1;
public class demo1{
public static void main(String[] args){
}
}
class PingpongMan implements SportMan{
@Override
public void rule(){
}
@Override
public void abrosd(){
}
@Override
public void run() {
}
@Override
public void competition(){
}
}
interface SportMan extends Law,Go{
void run();
void competition();
}
interface Law{
void rule();
}
interface Go{
void abrosd();
}
接口新增
package Demo1;
public class demo1{
public static void main(String[] args){
PingingMan zjk=new PingingMan();
zjk.run();
SportMan.inAddr();
}
}
class PingingMan implements SportMan{
}
interface SportMan {
default void run(){
go();
System.out.println("跑的贼快");
}
static void inAddr(){
System.out.println("在这里");
}
default void go(){
System.out.println("开始");
}
}
实现多个接口注意事项
接口和抽象类相似
静态代码块
package Demo1;
import java.util.ArrayList;
import java.util.List;
public class demo1{
public static List<String> cards=new ArrayList<>();
static {
System.out.println("静态代码块执行一次");
cards.add("3");
cards.add("4");
}
public static void main(String[] args){
System.out.println("main方法执行一次");
System.out.println(cards);
}
}
逼格高
实例代码块
final关键字
单例模式
package Demo1;
public class demo1{
public static void main(String[] args){
SingleIntance01 s1=SingleIntance01.getInstance();
SingleIntance01 s2=SingleIntance01.getInstance();
System.out.println(s1==s2);
}
}
class SingleIntance01{
public static SingleIntance01 ins=new SingleIntance01();
private SingleIntance01(){
}
public static SingleIntance01 getInstance(){
return ins;
}
}
package Demo1;
public class demo1{
public static void main(String[] args){
SingleIntance01 s1=SingleIntance01.getInstance();
SingleIntance01 s2=SingleIntance01.getInstance();
System.out.println(s1==s2);
}
}
class SingleIntance01{
public static SingleIntance01 ins=new SingleIntance01();
private SingleIntance01(){
}
public static SingleIntance01 getInstance(){
if(ins==null){
ins=new SingleIntance01();
}
return ins;
}
}
枚举类
常量
package Demo1;
public class demo1 {
public static final int up=0;
public static final int down=1;
public static final int left=2;
public static final int right=3;
public static void main(String[] args) {
move(right);
}
public static void move(int oritation){
switch (oritation){
case up:
System.out.println("上");
break;
case down:
System.out.println("下");
break;
case left:
System.out.println("左");
break;
case right:
System.out.println("右");
break;
}
}
}
枚举
package Demo1;
enum Oritation{
up,down,left,right;
}
public class demo1 {
public static void main(String[] args) {
move(Oritation.right);
}
public static void move(Oritation oritation){
switch (oritation){
case up:
System.out.println("上");
break;
case down:
System.out.println("下");
break;
case left:
System.out.println("左");
break;
case right:
System.out.println("右");
break;
}
}
}
第四天(多态、DATA、Object、正则)
API主要得记,直接用
多态
多态优势
package Demo1;
public class demo1 {
public static void main(String[] args){
Animal dlam=new Dog();
dlam.run();
Animal tatidi=new Dog();
go(tatidi);
Animal tom=new Cat();
go(tom);
}
public static void go(Animal d){
System.out.println("开始");
d.run();
System.out.println("结束");
}
class Dog extends Animal{
@Override
public void run(){
System.out.println("快");
}
public void lookdoor(){
System.out.println("看门");
}
}
class Cat extends Animal{
@Override
public void run(){
System.out.println("飞快");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
class Animal{
public void run(){
System.out.println("动物跑得快");
}
}
}
报错
多态自动类型转换
强制类型转换
有报错
package Demo1;
public class demo1 {
public static void main(String[] args){
Animal dlam=new Dog();
dlam.run();
Dog d=(Dog) dlam;
d.lookdoor();
Animal al=new Cat();
if (al instanceof Cat){
Cat c1=(Cat) al;
c1.catchMouse();
}else if(al instanceof Dog){
Dog d1=(Dog) a1;
d1.lookdoor();
}
}
public static void go(Animal d){
System.out.println("开始");
d.run();
System.out.println("结束");
}
class Dog extends Animal{
@Override
public void run(){
System.out.println("快");
}
public void lookdoor(){
System.out.println("看门");
}
}
class Cat extends Animal{
@Override
public void run(){
System.out.println("飞快");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
class Animal{
public void run(){
System.out.println("动物跑得快");
}
}
}
案例
package Demo1;
public class demo1 {
public static void main(String[] args){
Computer c=new Computer();
USB xiaomi=new Mouse("小米鼠标");
c.install(xiaomi);
// KeyBoard sfk=new KeyBoard("双飞燕键盘");
// c.install(sfy);
}
static class Computer{
public void install(USB usb){
usb.connect();
if(usb instanceof Mouse){
Mouse m=(Mouse) usb;
m.dbclick();
}else if(usb instanceof Mouse){
KeyBoard k=(KeyBoard) usb;
k.keyDown();
}
usb.unconnect();
}
}
static class Mouse implements USB{
private String name;
public Mouse(String name){
this.name=name;
}
public void dbclick(){
System.out.println(name+"老铁双击6666 ");
}
@Override
public void connect(){
System.out.println(name+"成功加入");
}
@Override
public void unconnect(){
System.out.println(name+"成功拔出");
}
}
static class KeyBoard implements USB{
private String name;
public KeyBoard(String name){
this.name=name;
}
@Override
public void connect(){
System.out.println(name+"加入");
}
@Override
public void unconnect(){
System.out.println(name+"拔出");
}
public void keyDown() {
System.out.println(name+"小老弟你不行啊");
}
}
interface USB{
void connect();
void unconnect();
}
}
跳过内部类局部匿名类58-64
包
Object类 String()
equals
objects类
日期类
package Demo1;
import java.util.Date;
public class demo1 {
public static void main(String[] args){
Date d=new Date();
System.out.println(d);
long time=d.getTime();
System.out.println(time);
}
}
package Demo1;
import java.util.Date;
public class demo1 {
public static void main(String[] args){
long startTime =new Date().getTime();
for(int i=1;i<1000000;i++){
System.out.println("输出:"+i);
}
long endTime=new Date().getTime();
System.out.println((endTime-startTime)/1000.0+"s");
}
}
SimpleDateFormat
package Demo1;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo1 {
public static void main(String[] args){
Date d=new Date();
System.out.println(d);
SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日||HH:mm:ss EEE a");
String rs= sdf.format(d);
System.out.println(rs);
}
}
面试例题
package Demo1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo1 {
public static void main(String[] args) throws ParseException {
String date="2019-11-04 09:30:30";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date newDate=sdf.parse(date);
long time =newDate.getTime()+(24L *60*60+15*60*60+30*60+29)*1000;
System.out.println(sdf.format(time));
}
}
日历类
有点问题待解决……
package Demo1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class demo1 {
public static void main(String[] args) throws ParseException {
Calendar rightNow=Calendar.getInstance();
System.out.println(rightNow);
int year =rightNow.get(Calendar.YEAR);
//月得加一
System.out.println(year);
int days=rightNow.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
// rightNow.set(Calendar.Year,2099);
// System.out.println(rightNow.get(Calendar.YEAR));
Date d=rightNow.getTime();
System.out.println(d);
long time= rightNow.getTimeInMillis();
System.out.println(time);
rightNow.add(Calendar.DAY_OF_YEAR,710);
long time = rightNow.getTimeInMillis();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
System.out.println(sdf.format(time1));
}
}
Math类
package Demo1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class demo1 {
public static void main(String[] args) throws ParseException {
System.out.println(Math.abs(10));
System.out.println(Math.abs(-10.3));
System.out.println(Math.ceil(4.000001));//5.0
System.out.println(Math.floor(4.9999999));//4.0
System.out.println(Math.pow(2,3));
System.out.println(Math.round(4.49999));//4
System.out.println(Math.round(4.500001));//5
}
}
系统类
大数据类
包装类
正则表达式
package Demo1;
public class demo1 {
public static void main(String[] args) {
System.out.println(checkQQ("2344124"));
}
//正则表达式
// public static boolean chechQQRegex(String qq){
// return qq!=null&&qq.matches("\\d{4,}");
// }
public static boolean checkQQ(String qq) {
if (qq == null) {
return false;
} else {
if (qq.length() >= 4) {
boolean rs = true;
for (int i=0;i<qq.length();i++){
char ch=qq.charAt(i);
if(ch<'0'||ch>'9'){
rs=false;
}
}
return rs;
} else {
return false;
}
}
}
}
校验
package Demo1;
import java.util.Scanner;
public class demo1 {
public static void main(String[] args) {
checkEmalil();
}
public static void checkEmalil() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入邮箱:");
String email=sc.nextLine();
if(email.matches("\\w{1,}@\\w{2,10}(\\.\\w{2,10}){1,2}")){
System.out.println("邮箱合法");
}else{
System.out.println("不合法");
}
}
}
方法
爬取
范型
约束操作的数据类型
自定义泛型
方法
泛意接口
通配符
集合
第五天(遍历、Set、斗地主游戏案例)
遍历
package Demo1;
import java.util.ArrayList;
import java.util.Collection;
public class demo1 {
public static void main(String[] args) {
Collection<String>lists =new ArrayList<>();
lists.add("赵敏");
lists.add("小昭");
lists.add("殷素素");
lists.add("周芷若");
System.out.println(lists);
for(String ele :lists){
System.out.println(ele);
}
//快捷键:lists.for 回车
int [] ages=new int[]{17,18,38,21};
for(int age:ages){
System.out.println(age);
}
}
}
也可以
数据结构总类
红黑树
树-二叉树-二叉查找树-平衡二叉树-红黑数
ArrayList集合
package lianxi;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args){
List<String> lists = new ArrayList<>();
lists.add("java1");
lists.add("java1");
lists.add("java2");
lists.add("java2");
System.out.println(lists);
//某个索引位置插入元素
lists.add(2,"MySQL");
System.out.println(lists);
//根据索引删除元素,返回被删除元素
System.out.println(lists.remove(2));
System.out.println(lists);
//根据索引获取元素
System.out.println(lists.get(2));
//修改索引位置处的元素
lists.set(3,"Mybatis");
System.out.println(lists);
}
}
队列
栈
set
斗地主案例
package lianxi;
public class Card {
private String number;
private String color;
public Card(){
}
public Card(String number, String color) {
this.number = number;
this.color = color;
}
/**
* 获取
* @return number
*/
public String getNumber() {
return number;
}
/**
* 设置
* @param number
*/
public void setNumber(String number) {
this.number = number;
}
/**
* 获取
* @return color
*/
public String getColor() {
return color;
}
/**
* 设置
* @param color
*/
public void setColor(String color) {
this.color = color;
}
public String toString() {
return number+color;
}
}
做牌
package lianxi;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Demo2 {
public static final List<Card> ALL_CARDS=new ArrayList<>();
static {
String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] colors={"黑桃","红心","梅花","方块"};
for (String number : numbers) {
for (String color : colors) {
Card card=new Card(number,color);
ALL_CARDS.add(card);
}
}
Collection.addAll(ALL_CARDS,new Card("","小王"),new Card("","大王"));
System.out.println("输出新牌:"+ALL_CARDS);
}
public static void main(String[] args){
}
}
洗牌
public static void main(String[] args){
Collections.shuffle(ALL_CARDS);
System.out.println("洗牌后:"+ALL_CARDS);
}
定义3玩家
list<Card> zhangsan1 = new ArrayList<>();
list<Card> zhangsan2 = new ArrayList<>();
list<Card> zhangsan3 = new ArrayList<>();
看牌
第六天
API与集合
链接
Stream
链接
IO输入输出
链接
File文件类
链接
反射
链接
第七天
自闭中……