在java中,如果有功能类似,可以使用相同的名字来定义不同功能方法;
定义重载方法的时候,要求:
1.方法的名字必须相同,作用域必须相同
2.参数必须不同:数量、类型、顺序不同(只要有一个不同,就是重载方法)
作用:
不用为了对不同的参数类型或参数个数,而写多个函数。多个函数用同一个名字,但参数表,即参数的个数或(和)数据类型可以不同,调用的时候,虽然方法名字相同,但根据参数表可以自动调用对应的函数。
public class A {
void test(){
System.out.println(“1”);
}
void test(int a){
System.out.println("a: "+a);
}
}
子类对父类的方法的一个重新实现
(1) 注解(Annotation)也被称为元数据(Metadata),用于修饰包、方法、属性、构造器、局部变量等数据信息。
(2) 注解不影响程序逻辑,但注解可以被编译或运行。
(3) 在JavaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在JavaEE中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替JavaEE旧版中所遗留的繁冗代码和XML配置等。
(1) @Override: 限定某个函数必须重写其他函数,该注解只能用于函数。函数名和参数列表必须相同。
(2) @Overload: 限定某个函数必须重载其他函数,该注解只能用于函数。函数名必须相同,参数列表必须不同。
(3) @Deprecated:用于表示某个程序元素(类、函数)已过时
(4) @SuppressWarnings:抑制编译器警告
// 对@override的使用
import java.util.Scanner;
class Point{
private int x;
private int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getString(){
return String.format(("%d %d"),x,y);
}
}
class ColorPoint extends Point{
public String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
@Override
public String getString(){
return String.format(("%s %s"),super.getString(),color);
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Point point2 = new ColorPoint(3,4,"red");
System.out.println(point2.getString());
}
}
//@Deprecated
package com.czl;
import org.w3c.dom.css.CSSUnknownRule;
import java.util.Scanner;
class Point{
public int x;
public int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
}
class ColorPoint extends Point{
private String color;
public ColorPoint(int x,int y,String color){
super(x,y);
this.color=color;
}
@Deprecated
public String getString(){
return String.format(("%d %d %s"),super.x,super.y,color);
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Point point2 = new ColorPoint(3,4,"red");
//我的本地编译器getString函数直接没了,没有划横线的
}
}
修饰其他注解的注解,就被称为元注解。
(1) Retention:指定注解的作用范围
(2) Target:指定注解可以用在哪些地方
(3) Document:注定注解是否出出现在javadoc中
(4) Inherited:子类会继承父类的注解
点开一个注解如图:
反射:动态引入类、动态调用实例的成员函数、成员变量等。
(1) java.lang.Class
(2) java.lang.reflect.Method
(3) java.lang.reflect.Field
(4) java.lang.reflect.Constructor
import java.util.Scanner;
class Worker extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Hello!"+this.getName());
}
try{
Thread.sleep(1000);
}catch (InterruptedException e){
throw new RuntimeException(e);
}
}
}
public class Main {
public static void main(String[] args) {
Worker worker1 = new Worker();
Worker worker2 = new Worker();
worker1.setName("thread-1");
worker2.setName("thread-2");
worker1.start();
worker2.start();
System.out.println("结束!!!");
}
}
//两个实例
import java.util.Scanner;
class Worker1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Hello"+"thread-1");
}
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Worker2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Hello"+"thread-2");
}
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
new Thread(new Worker1()).start();
new Thread(new Worker2()).start();
System.out.println("结束!!!");
}
}
//一个实例,方便写同步
import java.util.Scanner;
class Worker implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Hello"+i);
}
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
new Thread(new Worker()).start();
new Thread(new Worker()).start();
System.out.println("结束!!!");
}
}
start():开启一个线程
Thread.sleep(): 休眠一个线程
join():等待线程执行结束
interrupt():从休眠中中断线程
setDaemon():将线程设置为守护线程。当只剩下守护线程时,程序自动退出
lock:获取锁,如果锁已经被其他线程获取,则阻塞
unlock:释放锁,并唤醒被该锁阻塞的其他线程
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;
class Worker extends Thread {
public static int cnt = 0;
private static final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
for (int i = 0; i < 100000; i ++ ) {
lock.lock();
try {
cnt ++ ;
} finally {
lock.unlock();
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Worker worker1 = new Worker();
Worker worker2 = new Worker();
worker1.start();
worker2.start();
worker1.join();
worker2.join();
System.out.println(Worker.cnt);
}
}
class Worker extends Thread {
public static Integer cnt = 0;
private final static Object object = new Object();
@Override
public void run() {
synchronized (object){ //同步的变量不能变动
for(int i=0;i<10000;i++){
cnt++;
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Worker worker1 = new Worker();
Worker worker2 = new Worker();
worker1.start();
worker2.start();
worker1.join();
worker2.join();
System.out.println(Worker.cnt);
}
}
class Worker implements Runnable {
public static int cnt = 0;
private synchronized void work() {
for (int i = 0; i < 100000; i ++ ) {
cnt ++ ;
}
}
@Override
public void run() {
work();
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Worker worker = new Worker();
Thread worker1 = new Thread(worker);
Thread worker2 = new Thread(worker);
worker1.start();
worker2.start();
worker1.join();
worker2.join();
System.out.println(Worker.cnt);
}
}
大概就是操作系统中的阻塞和唤醒了
class Worker extends Thread{
private final Object obj;
private final boolean needWait;
public Worker(Object obj,boolean needWait){
this.obj=obj;
this.needWait = needWait;
}
@Override
public void run() {
synchronized (obj){
try {
if (needWait){
obj.wait();
System.out.println(this.getName()+":被唤醒了");
}else{
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main{
public static void main(String[] args) throws InterruptedException {
Object object = new Object();
for (int i = 0; i < 5; i ++ ) {
Worker worker = new Worker(object, true);
worker.setName("thread-" + i);
worker.start();
}
Worker worker = new Worker(object, false);
worker.setName("thread-" + 5);
Thread.sleep(1000);
worker.start();
}
}
上述内容参考AcWing