侧边栏壁纸
  • 累计撰写 8 篇文章
  • 累计收到 303 条评论

Java期末复习

hongyuxuan
2024-06-21 / 49 评论 / 196 阅读 / 正在检测是否收录...

基本数据类型及一些注意事项

逻辑类型:boolean

整数类型:byte, short, int, long

字符类型:char

浮点类型:float, double

需要加后缀的有longfloat

注意事项

  1. 对于局部变量,不存在默认值
  2. 只有实例方法才能调用实例变量

PATH和CLASSPATH的配置

PATH的配置: 找到JDK的安装路径(例如:D:\jdk),设置环境变量,在PATH中添加D:\jdk\bin

CLASSPATH的配置: 设置CLASSPATH环境变量,.; D:\jdk\lib

Java特点中面向对象是什么

Java是面向对象编程语言,面向对象编程(OOP)是一种编程范式,使用“对象”作为基本单元。对象包含数据(属性)和行为(方法)。

面向对象编程的三个主要特点:

  1. 封装:将对象的属性和方法封装在一起,隐藏对象的内部实现细节,只暴露必要的接口。这样可以保护数据,防止外部干扰。
  2. 继承:一种机制,可以创建一个新类,这个新类基于已有的类并继承其属性和方法。新类可以重用父类的代码,还可以添加新的功能。
  3. 多态:允许对象以多种形式出现。具体来说,一个父类的引用可以指向子类的实例,并且可以调用子类的方法,实现运行时多态。

各个修饰符修饰的类在各个场景下的访问权限

修饰符同一个类同一个包子类(同包)子类(不同包)其他包
public
protected×
默认 (无修饰符)××
private××××

说明

  • public:公共访问,任何地方都可以访问。
  • protected:受保护访问,同包可以访问,不同包的子类可以访问。
  • 默认(无修饰符):包级私有,同包可以访问,不同包不能访问。
  • private:私有访问,只有同一个类中可以访问。

  • 顶层类(Outer Class)只能用public或默认修饰,不能用protectedprivate
  • 内部类(Inner Class)可以用publicprotected、默认或private修饰。

成员变量

  • 成员变量可以用publicprotected、默认或private修饰,遵循上表的访问权限。

GUI中listener的用处

在Java的GUI编程中,listener是用于响应用户交互事件的接口。例如,点击按钮、移动鼠标等。listener可以让程序对用户操作做出响应。

常见的listener接口:

  • ActionListener: 用于处理动作事件(如按钮点击)。
  • MouseListener: 用于处理鼠标事件。
  • KeyListener: 用于处理键盘事件。

实际代码示例

  1. 写一个class,写构造方法,并继承:
class Animal {
    String name;

    // 构造方法
    public Animal(String name) {
        this.name = name;
    }

    public void makeSound() {
        System.out.println("Some sound...");
    }
}

class Dog extends Animal {
    // 子类的构造方法
    public Dog(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println("Bark");
    }

    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        dog.makeSound(); // 输出 "Bark"
    }
}
  1. 写一个接口,并写一个类实现该接口:
// 定义接口
public interface Vehicle {
    void start();
    void stop();
    int getSpeed();
}

// 实现接口的类
public class Car implements Vehicle {
    private int speed;

    @Override
    public void start() {
        System.out.println("Car is starting");
        speed = 10;
    }

    @Override
    public void stop() {
        System.out.println("Car is stopping");
        speed = 0;
    }

    @Override
    public int getSpeed() {
        return speed;
    }

    public static void main(String[] args) {
        Car car = new Car();
        car.start();
        System.out.println("Speed: " + car.getSpeed());
        car.stop();
        System.out.println("Speed: " + car.getSpeed());
    }
}
  1. 写一个抽象类以及实现:
abstract class Shape {
    // 抽象方法
    abstract void draw();

    // 非抽象方法
    void description() {
        System.out.println("This is a shape");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle");
    }

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.draw(); // 输出 "Drawing a circle"
        circle.description(); // 输出 "This is a shape"
    }
}
  1. super关键字:

在Java中,super 关键字是一个特殊的引用,它指向当前对象的父类。

class Parent {
    void show() {
        System.out.println("Parent's show()");
    }
    
    int value = 10;
}

class Child extends Parent {
    void show() {
        super.show(); // 调用父类的show()方法
        System.out.println("Child's show()");
    }
    
    int value = 20;
    
    void printValues() {
        System.out.println(super.value); // 访问父类的value字段
        System.out.println(value); // 访问子类的value字段
    }
    
    Child() {
        super(); // 调用父类的构造方法
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
        c.printValues();
    }
}

在这个例子中,Child 类继承自 Parent 类。在 Child 类的 show() 方法中,我们使用 super.show() 来调用 Parent 类的 show() 方法。在 printValues() 方法中,我们使用 super.value 来访问父类的字段,而直接使用 value 来访问子类的字段。在 Child 类的构造方法中,我们使用 super() 来调用父类的构造方法。

  1. 可变参数:

可变参数可以与普通参数一起使用,但可变参数必须放在参数列表的最后

public void printAll(Object first, int... numbers) {
    System.out.println(first);
    for (int num : numbers) {
        System.out.println(num);
    }
}

补充:2022级课堂作业

本部分材料来自lyz
  • JDK编译器:javac.exe
  • 正确main方法:public static void main(String args[])
  • jdk的安装目录为D:\jdk,则path的值设置为D:\jdk\bin,classpath的值设置为.; D:\jdk\lib
  • 源文件扩展名为.java,字节码的扩展名是.class
  • Java源文件是由若干个类组成的,这些类可以在一个源文件中,也可以分布在若干个源文件中,其中必须有一个源文件含有主类
  • 类体内容中声明成员变量是为了体现对象的属性还是行为?—-属性
  • 类体内容中定义的非构造方法是为了体现对象的属性还是行为?—-行为
  • 构造方法没有返回值
  • 构造方法可以重载
  • 类中的实例方法可以操作类变量(static变量)
  • 类方法(static方法)不需要操作实例变量
  • 类中的实例方法可以用类名不直接调用
  • 局部变量没有默认值
  • 接口中不可以声明变量
  • 接口中可以定义非抽象方法
  • 接口中的常量可以不指定初值
  • 可以在接口中只声明常量,不声明抽象方法
  • 允许接口中只有一个抽象方法
  • 线程在新建状态和死亡状态时,调用isAlive()方法返回值是false
  • FileInputStream按字节读取文件
  • 监视KeyEvent事件的监视器必须实现KeyListener接口
  • "Hello".equals(“hello”)的值是false
  • 监视WindowEvent事件的监视器必须实现WindowsListener接口
12

评论 (49)

取消
  1. 头像
    Windows X64 · QQ Browser

    画图

    回复
  2. 头像
    gzsdqnxbqf
    Windows 10 · Google Chrome

    对话设计自然,符合角色身份与情境。

    回复
  3. 头像
    fjxaoktmaz
    Windows 10 · Google Chrome

    选材新颖独特,通过细节描写赋予主题鲜活生命力。

    回复
  4. 头像
    mqxwxhixwz
    Windows 10 · Google Chrome

    作者的观点新颖且实用,让人在阅读中获得了新的思考和灵感。

    回复
  5. 头像
    kxolcznlat
    Windows 10 · Google Chrome

    这篇文章不错!

    回复
  6. 头像
    ihhliponyl
    Windows 10 · Google Chrome

    蓬莱仙踪

    回复
  7. 头像
    zfrhdjvizf
    Windows 10 · Google Chrome

    高飞不笨

    回复
  8. 头像
    bidsgdvpat
    Windows 10 · Google Chrome

    阿碧的恩典

    回复
  9. 头像
    ceabgkymkf
    Windows 10 · Google Chrome

    降临

    回复
  10. 头像
    ryuzqfgnsg
    Windows 10 · Google Chrome

    凤楼传之灵狐传说

    回复
  11. 头像
    pujpufzkee
    Windows 10 · Google Chrome

    诡摇铃

    回复
  12. 头像
    dnjxocgasa
    Windows 10 · Google Chrome

    女人街再见了

    回复
  13. 头像
    oybttrlvuj
    Windows 10 · Google Chrome

    九个半星期

    回复
  14. 头像
    qnxvzgevab
    Windows 10 · Google Chrome

    乒乓

    回复
  15. 头像
    xexiaeywvw
    Windows 10 · Google Chrome

    c++教程

    回复
  16. 头像
    tojercpwxj
    Windows 10 · Google Chrome

    恶魔恐怖现身

    回复
  17. 头像
    madiqojayd
    Windows 10 · Google Chrome

    莱拉

    回复
  18. 头像
    gfbstkmkim
    Windows 10 · Google Chrome

    第二幕

    回复
  19. 头像
    fqxcuvrmks
    Windows 10 · Google Chrome

    407航班

    回复
  20. 头像
    bleopkrfte
    Windows 10 · Google Chrome

    天生冤家

    回复
  21. 头像
    ajgvpxlzcm
    Windows 10 · Google Chrome

    照明商店

    回复
  22. 头像
    woyiuwvuut
    Windows 10 · Google Chrome

    呼吁

    回复
  23. 头像
    eaewziyoea
    Windows 10 · Google Chrome

    钢之炼金术师

    回复
  24. 头像
    jbyacjkryf
    Windows 10 · Google Chrome

    民间奇异志

    回复
  25. 头像
    mcjllanhbb
    Windows 10 · Google Chrome

    最佳损友粤配

    回复
  26. 头像
    oozaiqqfof
    Windows 10 · Google Chrome

    活埋求生

    回复
  27. 头像
    izpqelyznu
    Windows 10 · Google Chrome

    动物园天启夜

    回复
  28. 头像
    bgjkztburl
    Windows 10 · Google Chrome

    九月五日

    回复
  29. 头像
    exfgntvdym
    Windows 10 · Google Chrome

    乘风破浪

    回复
  30. 头像
    ntuwlnvjkl
    Windows 10 · Google Chrome

    赌神2

    回复
  31. 头像
    mtiwdyfnub
    Windows 10 · Google Chrome

    死亡电压

    回复
  32. 头像
    gktccwqerj
    Windows 10 · Google Chrome

    大冒险家粤配

    回复
  33. 头像
    pjlmntuhpd
    Windows 10 · Google Chrome

    聚光灯下的圣诞节

    回复
  34. 头像
    lptjjyemop
    Windows 10 · Google Chrome

    扎职

    回复
  35. 头像
    walnicpddh
    Windows 10 · Google Chrome

    闰年

    回复
  36. 头像
    zlzoprfzbp
    Windows 10 · Google Chrome

    下一站说爱你

    回复
  37. 头像
    fqolovpcdr
    Windows 10 · Google Chrome

    照明商店

    回复
  38. 头像
    otjdfvjkrm
    Windows 10 · Google Chrome

    柯村风云

    回复
  39. 头像
    cjtnavwcgb
    Windows 10 · Google Chrome

    隔墙有情人

    回复
  40. 头像
    frfghaboqi
    Windows 10 · Google Chrome

    她说

    回复
  41. 头像
    oxhhhbkqno
    Windows 10 · Google Chrome

    无限旅程

    回复
  42. 头像
    hhxllffygn
    Windows 10 · Google Chrome

    长江7号超萌特攻队

    回复
  43. 头像
    phqekraahh
    Windows 10 · Google Chrome

    浪潮

    回复
  44. 头像
    cnpjlhjxel
    Windows 10 · Google Chrome

    长津湖

    回复
  45. 头像
    pckzbrylfn
    Windows 10 · Google Chrome

    劫车王

    回复
  46. 头像
    ifhpwqxzfl
    Windows 10 · Google Chrome

    洪福齐天粤配

    回复
  47. 头像
    jyiqqqxdcq
    Windows 10 · Google Chrome

    拉姆齐范德堡案件

    回复
  48. 头像
    eqtwrtwlfv
    Windows 10 · Google Chrome

    哈泰利

    回复
  49. 头像
    maijyunvwm
    Windows 10 · Google Chrome

    鬼娃回魂

    回复