文章目录
Java关键字
用于定义数据类型的关键字 | |||||
class | interface | enum | byte | short | |
int | long | float | double | char | |
boolean | void | ||||
用于定义流程控制的关键字 | |||||
if | else | switch | case | default | |
while | do | for | break | continue | |
return | |||||
用于定义权限修饰符的关键字 | |||||
private | protected | public |
四种权限修饰符
修饰符 | 类内部 | 同一个包 | 不同包的子类 | 同一个工程 |
private | YES | |||
(缺省) | YES | YES | ||
protected | YES | YES | YES | |
public | YES | YES | YES | YES |
object
object是所有类的父类,一般省略不写
public class Study {
public static void main(String[] args) {
Object people=new People();
if(people instanceof Object){
People people1=(People)people;
people1.live();
}
}
}
class People extends Object{
public void live(){
System.out.println("人要吃饭");
}
}
包装类/基本数据类型
基本数据类型 | byte | short | int | long | float | double | boolean | char |
包装类 | Byte | Short | Intege | Long | Float | Double | Boolean | Character |
super
super用来调用父类的属性、方法和构造器
this 和 super 在构造器中,必须在子类构造器的第一行,且二者不能同时出现
public class Study {
public static void main(String[] args) {
Son son=new Son(12,"李华");
son.say();
}
}
class Father{
int age;
String name;
public Father(int age, String name) {
this.age = age;
this.name = name;
}
public void say(){
System.out.println(age+"岁的"+"父亲叫"+name);
}
}
class Son extends Father{
public Son(int age, String name) {//引用构造器
super(age, name);//引用父类构造器
}
public void say(){//重写say方法
System.out.println(age+"岁的"+"儿子叫"+name);
}
}
instanceof
instanceof关键字用于判断是否是子父类关系,返回值 trur / false
public class Study {
public static void main(String[] args) {
Father son=new Son();
son.walk();//只可以调用父类中的方法
// son.run();想要调用子类特有的方法只能强制转换
if(son instanceof Father){//
Son son1=(Son)son;//强制转换
((Son) son).run();
}
}
}
class Father{
public void walk(){
System.out.println("父亲要散步");
}
}
class Son extends Father{
public void run(){
System.out.println("儿子要跑步");
}
}
static
静态的,用来修饰属性、方法、代码块和内部类,随着类的加载而加载,可通过“类.静态变量”的方式调用
静态变量:创建多个对象,多个对象共享一个静态变量
public class Study {
public static void main(String[] args) {
Chinese a=new Chinese();
a.name="李华";
a.age=18;
// a.nation="中国";
Chinese.nation="中国";//static属性声明方法只能是“类.属性”
Chinese b=new Chinese();
b.name="小明";
b.age=30;
System.out.println(a.name+" "+a.age+" "+Chinese.nation);
System.out.println(b.name+" "+b.age+" "+Chinese.nation);
}
}
class Chinese{
String name;
int age;
static String nation;
}
final
最终的,修饰类、方法和变量
表示此类不可被继承,此方法不可被重写,变量不可被修改成为常量
public class Study {
public static void main(String[] args) {
final int a=10;
// a=20;不可修改
System.out.println(a);
Father father=new Father();
father.walk();
// Son son=new Son();
// son.walk();
}
}
final class Father{
int age;
final public void walk(){
System.out.println("父亲喜欢散步");
}
}
//class Son extends Father{不可被继承
// final public void walk(){
// System.out.println("儿子喜欢散步");
// }
//}
abstract
抽象方法:声明一个方法但不提供实现,该方法的实现由子类提供
抽象类:有一个或多个抽象方法的类
public class Study {
public static void main(String[] args) {
Students students=new Students();
students.work();
}
}
abstract class People{//抽象类
abstract public void work();//抽象方法
}
class Students extends People{
@Override
public void work() {//抽象方法由子类实现
System.out.println("学生的工作是学习");
}
}
interface
接口没有构造器,不可实例化
接口通过让类去实现的方法来使用
一个类可以有多个接口
public class Study {
public static void main(String[] args) {
Father son=new Son();
son.walk();//接口的多态写法
}
}
interface Father{
public void walk();
}
class Son implements Father{
public void walk(){
System.out.println("儿子要跑步");
}
}
synchronized(同步监视器)
synchronized():大括号中代码相当于单线程,多线程必须公用一个锁
public class ThreadTest {
public static void main(String[] args) {
Window t1 = new Window();
Window t2 = new Window();
Window t3 = new Window();
t1.setName("窗口一");
t2.setName("窗口二");
t3.setName("窗口三");
t1.start();
t2.start();
t3.start();
}
}
class Window extends Thread {
static Object obj=new Object();
private static int ticket=100;
@Override
public void run() {
while(true){
synchronized (obj){//锁,每次只能进入一个进程
if(ticket>0){
System.out.println(getName()+"卖出第"+ticket+"张票");
ticket--;
}
else{
break;
}
}//结束锁
}
}
}