您的位置:首页 > 编程语言 > Java开发

极客教程--Java基础内容(完结)

2021-06-05 12:25 274 查看

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(){
Sys
56c
tem.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 i
56c
nterface 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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: