哥伦布

java基础练习

 选择

复制代码
import java.util.Scanner;
//选择
public class SelectionAndLoop {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        int i = scanner.nextInt();
//        if(i>0){
//            System.out.println("您输入的是正数"+i);
//        }else if(i==0){
//            System.out.println("您输入的是0");
//        }else{
//            System.out.println("您输入的是负数");
//        }
        switch (i){
            case 1:
                System.out.println("输入为1");
                break;
            case 2:
                System.out.println("输入为2");
                break;
            default:
                System.out.println("输入既不是1也不是2");
                break;
        }

    }
}
复制代码

 循环

复制代码
import java.util.Scanner;
//循环
public class SelectionAndLoop {
    public static void main(String[] args) {
//        for(int i=0;i<10;i++){
//            System.out.println(i);
//        }

//        int i =0;
//        while (i<10){
//            System.out.println(i);
//            i++;
//        }

//        int i =0;
//        do{
//            System.out.println(i);
//            i++;
//        }while(i<10);

        int a[] = new int[]{1,2,3,4,5,6,7,8,9};
        for (int i : a) {
            System.out.println(i);
        }
    }
}
复制代码

 数组

复制代码
import java.util.Arrays;
//数组
public class Array {
    public static void main(String[] args) {
////        数组的初始化
////        动态赋值
//        int a[][] = new int[3][];
//        a[0] = new int[3];//第一行 3列
//        a[1] = new int[2];//第二行 2列
//        a[2] = new int[1];//第三行 1列
//        System.out.println(Arrays.toString(a[0]));//[0, 0, 0]

//        int b[][] = new int[3][2];
//        b[0][1]=1;
//        System.out.println(Arrays.toString(b[0]));//[0, 1]

////        静态赋值
//        int c[][] = new int[][]{{1,2,3},{9,8,7}};
//        System.out.println(Arrays.toString(c[0])); //[1, 2, 3]


////        数组的声明
////        int arr[];
////        数组的初始化
//        int arr1[] = {1,2,3,4};
//        int arr2[] = new int[]{1,2,3,4};
////        查看数组的长度
//        System.out.println(arr2.length); //4
////        each循环
//        for(int i:arr2){
//            System.out.print(i); //1234
//        }


////        数组的拷贝
//        int arr[] = new int[]{1,2,3,4};
//        int a[] = arr;
//        System.out.println(Arrays.toString(a)); //[1, 2, 3, 4]
////        指定长度进行拷贝
//        int b[] = Arrays.copyOf(arr,2);
//        System.out.println(Arrays.toString(b));//[1, 2]

////        数组的排序
//        int arr[] = {100,23,4,0};
//        Arrays.sort(arr);
//        System.out.println(Arrays.toString(arr));//[0, 4, 23, 100]

////        将int整形数组转换成为字符串数组
//        Arrays.toString(arr)


    }

}
复制代码

 输入和输出

复制代码
import java.util.Scanner;
//输入和输出
public class InputAndOutput {
    public static void main(String[] args) {
//        s.nextInt() 获取整形
//        s.nextLine() 获取字符串
//        s.next() 获取字符串
//        s.nextFloat() 获取Float
//        输入
        Scanner s = new Scanner(System.in);
        System.out.println("请输入:");
        String a =s.next();
//        输出
        System.out.println("我换行输出了");
        System.out.print("我没换行输出了");
        System.out.println("");
        System.out.printf("输入的值为 %s",a);

    }
}
复制代码

 类与对象

复制代码
//类与对象
class ClassAndObject extends Student implements UserDao{
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("nanke");
        System.out.println(s1.getName());//nanke
    }
}
class Student{
    //私有属性 成员变量
    private String name;
    //无参构造器
    public Student() {
    }
    //有参构造器
    public Student(String name) {
        this.name = name;
    }
    //Get
    public String getName() {
        return name;
    }
    //Set
    public void setName(String name) {
        this.name = name;
    }
    //function
    public void myPrint(){
        System.out.println("输出");
    }
}
复制代码

类的继承

复制代码
//类的继承
public class Parent {
    private int age;

    public Parent() {
    }

    public Parent(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public void myPrint(){
        System.out.println("我是父类方法");
    }
}

class Son extends Parent{
    public void myPrint(){
        System.out.println("我重写了父类的方法");
    }
}
class isx{
    public static void main(String[] args) {
        Son s1 = new Son();
        s1.setAge(22);//一般通过set方法修改私有属性
        System.out.println(s1.getAge());//22
        s1.myPrint();//子类的方法比父类优先级高 //我重写了父类的方法
    }
}
复制代码

 构造方法

复制代码
//构造方法
class Rl{
    private int age;
    private String name;
    //无参
    public Rl() {
    }
    //重载
    public Rl(int age) {
        this.age = age;
    }

    public Rl(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

class isx{
    public static void main(String[] args) {
        Rl p = new Rl(12,"nanke");
        System.out.println(p.getAge());
        System.out.println(p.getName());
    }
}
复制代码

 super

复制代码
//super
public class Jc {
    public void myPrint(){
        System.out.println("我是父类方法");
    }
}

class Son extends Jc{
    public void myPrint(){
        super.myPrint();
        System.out.println("我重写了父类的方法");
    }
}
class s{
    public static void main(String[] args) {
        Son s1 = new Son();
        s1.myPrint();
//        我是父类方法
//        我重写了父类的方法
    }
}
复制代码

static和final

复制代码
//static和final
public class Jc {
    //    静态代码块
    static {
        int a = 3;
    }
    private static String name = "nanke";
    public static void myPrint(){
        System.out.println("我的方法");
    }
}

class Ac {
    public static void main(String[] args) {
        //加了static后可以不实例化直接calss调用方法
        Jc.myPrint();//我的方法
    }
}
//final修饰的类不能被继承
final class Ha{
//    final修饰的方法不能被重写 但是子类可以用父类的final修饰方法
//    基本数据类型使用final修改的变量就不可变了
//    引用类型的数据被final修饰:引用变量引用不可变,但是引用对象的内容可以改变
    final void jj(){
        System.out.println("jj");
    }
}
复制代码

abstract抽象

复制代码
//abstract抽象
//抽象类
public abstract class Abstractpp {
//    抽象类的方法不可有方法体
    public abstract void myPrint();
}
class As extends Abstractpp{
    //子类这里不可继承抽象方法 必须自己重写
    @Override
    public void myPrint() {
        System.out.println("你好");
    }
}
复制代码

 接口

复制代码
public interface UserDao {
    //接口中所有定义的方法中其实都是抽象的 pubilc abstract
    //变量只能为 pubilc static final类型的
    //void add();
    //等效于 public abstract final void add();
    //int age = 99;
    //等效于 pubilc static final int age =99;
    int age =33;
    void myPrint();
    void insert(a);
}

-------------------------------------------------------------------
public class UserTest implements UserDao {
    public void myPrint(){
        System.out.println("nihao");
    }
    public void insert(a){
        System.out.println("新增");
    }
}
复制代码

 多态

复制代码
//多态
//特征
//1.继承
//2.重写
//3.父类引用指向子类对象(首先检查父类中是否有该方法,如果没有,则编译错误;如果有,再去调子类的同名方法)
public class Test {
    public static void main(String[] args) {
        //向上转型 (小的给大的)
//        Parent parent = new Son();//通过子类去实例化父类
//        parent.myPrint();

        //向下转型 (大的给小的)
        //如果子类有方法 使用向上转型调用eat会报错 但是向下转型就可以调用特有的
        Parent parent = new Son(); //由于java的安全型问题 你不能直接通过向下转型 而是要通过向上转型后成一个对象再向下转型
        Son son =(Son) parent;
        son.eat();//子类正在吃饭
    }
}

class Parent{
    public void myPrint(){
        System.out.println("我是父类");
    }
}

class Son extends Parent{
    public void myPrint(){
        System.out.println("我是子类");
    }
    public void eat(){
        System.out.println("子类正在吃饭");
    }
}
复制代码

异常

复制代码
//异常一
public class Test2 {
    public static void main(String[] args) {
        try{
            int a = 10/0;
            System.out.println("我是try");
        }catch (Exception e){
            System.out.println("异常为:"+e);
        }finally {
            System.out.println("无论有无异常我都会执行");
        }
//        异常为:java.lang.ArithmeticException: / by zero
//        无论有无异常我都会执行

    }
}
复制代码
//异常二
public class Test2 {
    public static void main(String[] args) throws Exception{
        int a =10/0;
//        Exception in thread "main" java.lang.ArithmeticException: / by zero
//        at Test2.main(Test2.java:4)
    }
}
复制代码
//异常三
public class Test2 {
    public static void main(String[] args) {
        try{
            int a = 10/0;
            System.out.println("我是try");
        }catch (Exception e){
            throw  e;
        }finally {
            System.out.println("无论有无异常我都会执行");
        }
//        Exception in thread "main" java.lang.ArithmeticException: / by zero
//        at Test2.main(Test2.java:5)
//        无论有无异常我都会执行
    }
}
复制代码
复制代码
import java.util.Scanner;
//自定义异常
public class Test2 {
    public static void main(String[] args) throws MyException{
        System.out.println("请输入年龄:");
        Scanner scanner = new Scanner(System.in);
        int age = scanner.nextInt();
        if(age<0){
            throw new MyException("年龄最小为0,请重新输入");
        }else{
            System.out.println(age+"您的年龄合法");
        }
    }
}
class MyException extends Exception{
    public MyException(){

    }
    public MyException(String msg){
        super(msg);
    }
}

//请输入年龄:
//        -2
//        Exception in thread "main" MyException: 年龄最小为0,请重新输入
//        at Test2.main(Test2.java:10)

//请输入年龄:
//        10
//        10您的年龄合法
复制代码

 StringBuffer

复制代码
//String stringBuffer Random Math Integer
import java.util.Arrays;
import java.util.Random;

public class StringMethod {
    public static void main(String[] args) {
        //String的初始化
        String str = "hello";
        String str2 = new String();
        str2 = " hello ";
        String str3 = new String("hello");

        //String的常用内置方法
        //返回指定下标的值
        System.out.println(str.charAt(0));//h
        //字符串拼接
        System.out.println(str.concat("world"));//helloworld
        //比较相同
        System.out.println(str.equals("haha"));//false
        //返回值对应的下标 无则-1
        System.out.println(str.indexOf("o"));//4
        //从第几位开始检索值对应的下标
        System.out.println(str.indexOf('l',3));//3
        //是否以xxx为开头
        System.out.println(str.startsWith("hell"));//true
        //是否以xxx结尾
        System.out.println(str.endsWith("lo"));//true
        //单字符数组
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
        //去除字符串前后空白
        System.out.println(str2.trim());

        //stringBuffer
        StringBuffer sb = new StringBuffer("helloworld");
        sb.append("99");//helloworld99
        sb.insert(1,"99");//h99elloworld99
        sb.delete(0,3);//elloworld99
        sb.reverse();//颠倒
        System.out.println(sb);

        //Random
//        Random random = new Random();
//        for (int i = 0;i<10;i++){
//            System.out.println(random.nextInt(11));//[0,11]->0~10
//        }

        //Math
        //绝对值
        System.out.println(Math.abs(-10));//10
        //计算平方根
        System.out.println(Math.sqrt(2));//1.4142135623730951
        //计算立方根
        System.out.println(Math.cbrt(3));//1.4422495703074083
        //计算a的b次方
        System.out.println(Math.pow(2, 3));//8.0
        //计算最大值
        System.out.println(Math.max(3, 10));//10
        //计算最小值
        System.out.println(Math.min(3, 10));//3
        //接近此数的大的整数的值
        System.out.println(Math.ceil(3.1));//4.0
        //接近此书的小的整数的值
        System.out.println(Math.floor(3.6));//3.0
        //随机数 默认0到1的值
        System.out.println(Math.random()*10);//[0,1)
        //四舍五入
        System.out.println(Math.round(4));


        //自动装箱
        Integer i =1;
        //自动拆箱
        int m = i;
        System.out.println(m);
    }
}
复制代码

 ArrayList

复制代码
//ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Test3 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        //增加
        arrayList.add("1");
        arrayList.add(2);
        arrayList.add(2.2);
        System.out.println(arrayList.get(0));//1
        //通过iterator构造器访问
//        Iterator iterator = arrayList.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//            1
//            2
//            2.2

        //修改
//        arrayList.remove("1");//删除了1
//        Iterator iterator = arrayList.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }

        //长度
        System.out.println(arrayList.size());
        //是否包含
        System.out.println(arrayList.contains(2.2));
    }
}
复制代码

 HashMap

复制代码
//HashMap
import java.util.*;

public class Hm {
    public static void main(String[] args) {
        HashMap hashMap =new HashMap();
        //
        hashMap.put("name","张旭");
        hashMap.put("lover","自己");

        //查询
        String name =(String) hashMap.get("name");
        System.out.println(name);//张旭

        //查询所有的key
        Set set = hashMap.keySet();
        System.out.println(set);//[lover, name]

        //查询所有的value
        Collection values = hashMap.values();
        System.out.println(values);//[自己, 张旭]

        //遍历
//        Iterator iterator = values.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
        //自己
        //张旭

        //输出key和value
        Set<Map.Entry<String,String>> entrySet = hashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //lover=自己
        //name=张旭

        //修改
        hashMap.put("name","nanke");
        System.out.println(hashMap);//{lover=自己, name=nanke}

        //删除
        hashMap.remove("name");
        System.out.println(hashMap);//{lover=自己}

        //是否为空
        System.out.println(hashMap.isEmpty());//false

        //是否包含key
        System.out.println(hashMap.containsKey("sss"));//false
        //是否包含value
        System.out.println(hashMap.containsValue("自己"));//true
    }
}
复制代码

 线程

复制代码
//线程
public class Thread1 extends Thread{
    public void  run(){
        for (int i = 0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}
class Thread2 implements Runnable{

    @Override
    public void run() {
        for (int i = 10;i<20;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}

class Testss{
    public static void main(String[] args) {
        //线程1
        Thread1 thread1 =new Thread1();
        thread1.start();
        //线程2
        Thread2 thread2 = new Thread2();
        Thread thread = new Thread(thread2);
        thread.start();
        System.out.println("main线程开始");
        for(int i=0;i<30;i++){
            System.out.println("main线程"+i);
        }
    }
}
//可以发现每次的运行结果都不一样
//Thread-0   i=0
//Thread-0   i=1
//Thread-0   i=2
//Thread-0   i=3
//Thread-0   i=4
//Thread-0   i=5
//Thread-0   i=6
//Thread-0   i=7
//Thread-0   i=8
//Thread-0   i=9
//main线程开始
//main线程0
//main线程1
//main线程2
//Thread-1   i=10
//main线程3
//Thread-1   i=11
//main线程4
//Thread-1   i=12
//main线程5
//Thread-1   i=13
//main线程6
//Thread-1   i=14
//main线程7
//main线程8
//main线程9
//main线程10
//main线程11
//main线程12
//main线程13
//main线程14
//main线程15
//Thread-1   i=15
//main线程16
//Thread-1   i=16
//main线程17
//main线程18
//main线程19
//main线程20
//main线程21
//main线程22
//main线程23
//main线程24
//main线程25
//main线程26
//main线程27
//main线程28
//main线程29
//Thread-1   i=17
//Thread-1   i=18
//Thread-1   i=19
复制代码

 线程join的使用

复制代码
//线程
public class Thread1 extends Thread{
    public void  run(){
        for (int i = 0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}
class Thread2 implements Runnable{

    @Override
    public void run() {
        for (int i = 10;i<20;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}

class Testss{
    public static void main(String[] args) throws InterruptedException {
        //线程1
        Thread1 thread1 =new Thread1();
        thread1.start();
        //线程2
        Thread2 thread2 = new Thread2();
        Thread thread = new Thread(thread2);
        thread.start();
        thread.join(); // 等待以上线程执行完毕
        System.out.println("main线程开始");
        for(int i=0;i<30;i++){
            System.out.println("main线程"+i);
        }
    }
}
//可以看到 main线程等待了上面两个线程的执行
//Thread-0   i=0
//Thread-0   i=1
//Thread-0   i=2
//Thread-0   i=3
//Thread-0   i=4
//Thread-1   i=10
//Thread-1   i=11
//Thread-0   i=5
//Thread-0   i=6
//Thread-0   i=7
//Thread-0   i=8
//Thread-0   i=9
//Thread-1   i=12
//Thread-1   i=13
//Thread-1   i=14
//Thread-1   i=15
//Thread-1   i=16
//Thread-1   i=17
//Thread-1   i=18
//Thread-1   i=19
//main线程开始
//main线程0
//main线程1
//main线程2
//main线程3
//main线程4
//main线程5
//main线程6
//main线程7
//main线程8
//main线程9
//main线程10
//main线程11
//main线程12
//main线程13
//main线程14
//main线程15
//main线程16
//main线程17
//main线程18
//main线程19
//main线程20
//main线程21
//main线程22
//main线程23
//main线程24
//main线程25
//main线程26
//main线程27
//main线程28
//main线程29
复制代码

 线程sleep的使用

复制代码
//线程
public class Thread1 extends Thread{
    public void  run(){
        for (int i = 0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}
class Thread2 implements Runnable{

    @Override
    public void run() {
        //等待线程2睡眠3秒才执行
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 10;i<20;i++){
            System.out.println(Thread.currentThread().getName()+"   i="+i);
        }
    }
}

class Testss{
    public static void main(String[] args) throws InterruptedException {
        //线程1
        Thread1 thread1 =new Thread1();
        thread1.start();
        //线程2
        Thread2 thread2 = new Thread2();
        Thread thread = new Thread(thread2);
        thread.start();
        System.out.println("main线程开始");
        for(int i=0;i<30;i++){
            System.out.println("main线程"+i);
        }
    }
}
//可以看到Thread-1线程真的等待了3秒后才执行
//Thread-0   i=0
//Thread-0   i=1
//Thread-0   i=2
//Thread-0   i=3
//Thread-0   i=4
//Thread-0   i=5
//Thread-0   i=6
//Thread-0   i=7
//Thread-0   i=8
//Thread-0   i=9
//main线程开始
//main线程0
//main线程1
//main线程2
//main线程3
//main线程4
//main线程5
//main线程6
//main线程7
//main线程8
//main线程9
//main线程10
//main线程11
//main线程12
//main线程13
//main线程14
//main线程15
//main线程16
//main线程17
//main线程18
//main线程19
//main线程20
//main线程21
//main线程22
//main线程23
//main线程24
//main线程25
//main线程26
//main线程27
//main线程28
//main线程29
//Thread-1   i=10
//Thread-1   i=11
//Thread-1   i=12
//Thread-1   i=13
//Thread-1   i=14
//Thread-1   i=15
//Thread-1   i=16
//Thread-1   i=17
//Thread-1   i=18
//Thread-1   i=19
复制代码

 编程题1

复制代码
public class Areatext {
    public static void main(String[] args) {
        Rectangle rc = new Rectangle(5.5f,3.2f);
        System.out.println("周长为:"+ rc.zhouchang());
        System.out.println("面积为:"+rc.mianji());
    }
}

class  Rectangle{
    private float len;
    private float wid;
    public Rectangle(){

    }
    public Rectangle(float len, float wid){
        this.len = len;
        this.wid = wid;

    }
    public float zhouchang(){
        return (len+wid)*2;
    }
    public float mianji(){
        return len*wid;
    }
}
复制代码

 编程题2

复制代码
public interface TrafficTool {
    void start();

    void stop();
}

class Bike implements TrafficTool {
    @Override
    public void start() {
        System.out.println("aaa");
    }

    @Override
    public void stop() {
        System.out.println("bbb");
    }
}

class Bus implements TrafficTool {
    @Override
    public void start() {
        System.out.println("ccc");
    }

    @Override
    public void stop() {
        System.out.println("ddd");
    }
}

class Testss {
    public static void main(String[] args) {
        Bike b = new Bike();
        b.start();
        b.stop();
        Bus c = new Bus();
        c.start();
        c.stop();
    }

}
复制代码

 

posted @   南柯Dream丶  阅读(43)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
历史上的今天:
2020-11-10 浏览器Storage
2020-11-10 js-cookie用法详解
点击右上角即可分享
微信分享提示