类和对象

Day 3-5:变量、数据类型和运算符

1. 基本数据类型

在 Java 中,有以下基本数据类型:

整数类型:byte、short、int、long
浮点类型:float、double
字符类型:char
布尔类型:boolean
了解每种数据类型的取值范围和用途。例如:

int number = 42;
double pi = 3.14;
char grade = 'A';
boolean isJavaFun = true;

2. 变量

学习如何声明和使用变量:

// 声明一个整数变量
int age;

// 初始化变量
age = 25;

// 同时声明和初始化变量
double height = 1.75;

3. 常量

了解常量的概念,使用 final 关键字声明常量:

final double PI = 3.14159265359;

4. 命名规范

遵循 Java 的命名规范,使用有意义的变量名:

int studentAge;
double totalPrice;
char firstLetter;

第4天:算术运算符

学习 Java 中的算术运算符,包括:

+:加法
-:减法
*:乘法
/:除法
%:取模(取余)

int a = 10;
int b = 4;

int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;

第5天:关系和逻辑运算符

1. 关系运算符

学习关系运算符,用于比较两个值的关系:

  • ==:相等
  • !=:不相等
  • :大于

  • <:小于
  • =:大于等于

  • <=:小于等于
int x = 5;
int y = 8;

boolean isEqual = (x == y);
boolean isNotEqual = (x != y);
boolean isGreaterThan = (x > y);
boolean isLessThan = (x < y);
boolean isGreaterOrEqual = (x >= y);
boolean isLessOrEqual = (x <= y);

2. 逻辑运算符

了解逻辑运算符,用于组合多个条件:

  • &&:逻辑与(and)
  • ||:逻辑或(or)
  • !:逻辑非(not)
boolean condition1 = true;
boolean condition2 = false;

boolean resultAnd = condition1 && condition2;
boolean resultOr = condition1 || condition2;
boolean resultNot = !condition1;

练习:

声明两个整数变量,分别赋予初始值,然后使用算术运算符计算它们的和、差、积、商和余数。
使用关系运算符比较两个数的大小,并将结果输出。
使用逻辑运算符组合两个条件,输出结果。

package top.srefinops.xbuivr;

public class asdf {
    public static void main(String[] args) {
        // 声明两个整数变量并赋值
        int num1 = 10;
        int num2 = 5;

        // 使用算术运算符计算和、差、积、商和余数
        int sum = num1 + num2;
        int difference = num1 - num2;
        int product = num1 * num2;
        int quotient = num1 / num2;
        int remainder = num1 % num2;

        // 输出结果
        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
        System.out.println("Remainder: " + remainder);

        // 使用关系运算符比较两个数的大小
        boolean isGreaterThan = num1 > num2;
        boolean isLessThan = num1 < num2;
        boolean isEqual = num1 == num2;

        // 输出比较结果
        System.out.println("Is num1 greater than num2? " + isGreaterThan);
        System.out.println("Is num1 less than num2? " + isLessThan);
        System.out.println("Is num1 equal to num2? " + isEqual);

        // 使用逻辑运算符组合两个条件
        boolean condition1 = num1 > 0;
        boolean condition2 = num2 < 0;

        // 使用逻辑与运算符
        boolean resultAnd = condition1 && condition2;

        // 输出逻辑运算结果
        System.out.println("Result of condition1 AND condition2: " + resultAnd);
    }
}
已连接到目标 VM, 地址: ''127.0.0.1:59890',传输: '套接字''
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Is num1 greater than num2? true
Is num1 less than num2? false
Is num1 equal to num2? false
Result of condition1 AND condition2: false
与目标 VM 断开连接, 地址为: ''127.0.0.1:59890',传输: '套接字''

Day 6-7:控制流结构

下面是有关if语句、switch语句、for循环和while循环的基本使用。

if语句

if 语句允许你根据某个条件选择性地执行代码块。

public class IfStatementExample {
    public static void main(String[] args) {
        int x = 10;

        // if语句
        if (x > 5) {
            System.out.println("x大于5");
        } else {
            System.out.println("x不大于5");
        }
    }
}

switch语句

switch 语句用于根据不同的情况执行不同的代码。

public class SwitchStatementExample {
    public static void main(String[] args) {
        int dayOfWeek = 3;

        // switch语句
        switch (dayOfWeek) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            default:
                System.out.println("其他天");
        }
    }
}

for循环

for 循环用于重复执行一段代码。

public class ForLoopExample {
    public static void main(String[] args) {
        // for循环
        for (int i = 0; i < 5; i++) {
            System.out.println("当前i的值:" + i);
        }
    }
}

while循环

while 循环在给定条件为真时重复执行代码块。

public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 0;

        // while循环
        while (count < 5) {
            System.out.println("当前count的值:" + count);
            count++;
        }
    }
}

练习

假设有一个整数数组,需要编写一个程序,计算数组中所有偶数的平均值。

package top.srefinops.xbuivr;

import java.util.Arrays;

public class day2 {
    public static void main(String[] args) {
        testMaxDay();
        averageOfEvenNumbers();
    }
    // 获取一个数组里面的最大值
    public static void testMaxDay() {
        // 给定数组
        int[] numbers = {23, 45, 12, 67, 89, 34, 56};
        // 使用Stream API找到最大值
        int max = Arrays.stream(numbers).max().orElse(0);
        System.out.println("数组中的最大值是: " + max);
    }

    // 假设有一个整数数组,你需要编写一个程序,计算数组中所有偶数的平均值
    public static void averageOfEvenNumbers() {
        // 给定整数数组
        int[] numbers = {23, 45, 12, 67, 89, 34, 56, 78, 90};
        // 计算偶数的平均值
        int sum = 0;
        int count = 0;
        for (int num : numbers) {
            sum += num;
            count++;
        }
        // 避免除零错误
        double average = count > 0 ? (double) sum / count : 0;

        // 输出结果
        System.out.println("数组中所有偶数的平均值是: " + average);
    }
}

Day 8-10:类和对象

了解什么是类和对象,如何定义类和创建对象。
当我们说到面向对象编程(OOP)中的类和对象时,我们实际上是在谈论一种编程范式,它将数据和操作数据的方法组织为单个单元 - 对象。让我们详细了解类和对象的概念:

类和对象

  • 类(Class): 类是一种蓝图或模板,用于创建对象的定义。它定义了对象的属性(也称为成员变量)和方法(也称为成员方法)。

    public class Car {
        // 成员变量
        String brand;
        int year;
        
        // 成员方法
        void start() {
            System.out.println("The car is starting.");
        }
        
        void drive() {
            System.out.println("The car is in motion.");
        }
    }

    在这个例子中,Car 类定义了汽车对象的属性(品牌和年份)和方法(启动和行驶)。

  • 对象(Object): 对象是类的实例。它是类定义的具体实体,具有类中定义的属性和方法。可以创建多个对象,每个对象都有自己的状态和行为。

    public class Main {
        public static void main(String[] args) {
            // 创建 Car 类的对象
            Car myCar = new Car();
            
            // 设置对象的属性
            myCar.brand = "Toyota";
            myCar.year = 2023;
            
            // 调用对象的方法
            myCar.start();
            myCar.drive();
        }
    }

    在这个例子中,myCar 是 Car 类的一个对象。通过对象访问成员变量和调用成员方法。

如何定义类和创建对象

  1. 定义类: 使用 class 关键字来声明一个类,然后在大括号内定义类的成员变量和成员方法。

    public class ClassName {
        // 成员变量
        dataType variableName;
        
        // 成员方法
        returnType methodName() {
            // 方法体
        }
    }
  2. 创建对象: 使用 new 关键字创建类的实例,也就是对象。

    ClassName objectName = new ClassName();

测试

当然,以下是一个关于类和对象的小测试。请尝试回答这些问题,如果有不清楚的地方,随时告诉我。

小测试题

  1. 什么是类?

    a. 一段代码
    b. 一个变量
    c. 一个对象的集合
    d. 一个创建对象的模板

  2. 什么是对象?

    a. 一个方法
    b. 一个变量
    c. 一个类的实例
    d. 一组属性

  3. 如何声明一个类?

    a. class MyClass {}
    b. new MyClass();
    c. MyClass object = new MyClass();
    d. class {MyClass}

  4. 如何创建一个对象?

    a. new MyClass();
    b. class MyClass = new Object();
    c. MyClass object = new MyClass();
    d. Object myObject = new MyClass();

  5. 一个类可以有多个对象吗?

    a. 是的
    b. 不可以,每个类只能有一个对象
    c. 只有在特殊情况下可以有多个对象
    d. 取决于类的定义

答案

  1. d. 一个创建对象的模板
  2. c. 一个类的实例
  3. a. class MyClass {}
  4. c. MyClass object = new MyClass();
  5. a. 是的






posted @ 2023-11-16 01:30  Security  阅读(14)  评论(0编辑  收藏  举报
小布的个人空间 京ICP备2023021324号-1