Java--基礎內容(一)
之前有贊筆試題基礎題掛了,還是得承認自己基礎很垃圾,要多補補,不能只搞框架。精通基礎,結合設計模式,以後能走得更遠
極客教程--參考鏈接:https://geek-docs.com/java/java-tutorial/super-keyword-in-java-with-example.html
Java
Java特性
抽象:子類要實現父類所有方法
封裝:類封裝屬性
繼承:子類擁有父類所有能力,並且可以拓展,實現多態效果
多態:多態允許您定義一個接口並具有多個實現
訪問修飾符
super關鍵字
子類 new 的時候,需要調用父類的構造方法,也可以用super訪問父類變量和父類構造函數
重載與重寫
overload:類中函數名相同,但是參數和返回值不同
override:子類繼承父類:對父類方法的重寫
接口與抽象類
父類 = new 子類:只能使用子類繼承過來的方法,且方法是子類的
抽象類:所有繼承的子類必須實現父類中所有抽象類方法,且爲public
接口中可以包含抽象類,A類不能多重繼承實現多種功能,但是可以通過多重實現接口來實現多種功能
垃圾回收:
垃圾回收堆內存,當對象 = null(不可達),或者引用obj1 = 引用obj2時就要垃圾回收
package com.empirefree.springboot;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.awt.*;
/**
* @program: springboot
* @description: Java基礎
* @author: huyuqiao
* @create: 2021/06/05 10:14
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class JavaTest {
/*
* 子類new,先調用父類的構造方法
* */
class Parentclass
{
int num = 100;
//no-arg constructor
Parentclass(){
System.out.println("no-arg constructor of parent class");
}
//arg or parameterized constructor
Parentclass(String str){
System.out.println("parameterized constructor of parent class");
}
void display(){
System.out.println("Hello");
}
}
class Subclass extends Parentclass
{
Subclass(){
/* super() must be added to the first statement of constructor
* otherwise you will get a compilation error. Another important
* point to note is that when we explicitly use super in constructor
* the compiler doesn't invoke the parent constructor automatically.
*/
// super("Hahaha");
System.out.println("Constructor of child class");
System.out.println(super.num);
}
}
abstract class AbstractDemo{
public void myMethod(){
System.out.println("hello");
}
abstract public void anotherMethod() throws Exception;
void test(){
System.out.println("asdf");
}
}
public class Demo extends AbstractDemo{
public void anotherMethod() throws Exception {
System.out.println("Abstract method");
throw new MyException("拋出異常。。。");
}
}
public interface Inf1{
}
public interface Inf2 extends Inf1{
}
//自定義異常
class MyException extends Exception{
MyException(String str2) {
System.out.println("asdfs");
}
}
public class SimpleExample extends Frame{
SimpleExample(){
//玩玩awt
Button button = new Button("button");
button.setBounds(50, 50, 50, 50);
add(button);
setSize(500, 300);
setTitle("this is my first awt example");
setLayout(new FlowLayout());
setVisible(true);
}
}
@Test
public void testParentAndChildren() throws Exception {
/* Subclass obj= new Subclass();
obj.display();*/
/*
父類 = new 子類:只能使用子類繼承過來的方法,且方法是子類的
抽象類:所有繼承的子類必須實現父類中所有抽象類方法
*/
/* AbstractDemo abstractDemo = new Demo();
abstractDemo.anotherMethod();
abstractDemo.test();*/
System.setProperty("java.awt.headless", "false");
SimpleExample simpleExample = new SimpleExample();
}
}