第四章 重载&访问修饰符&静态&常用类 ② 代码

1.课前测试 学生类

1.创建学生类
包括:
属性:姓名,年龄,电话,爱好
方法1:自我介绍(无参,无返回值)
输出姓名,年龄,电话,爱好
方法2:上网(有参,无返回值)
参数:上网小时,每小时多少钱
输出:上网多长时间,花了多少钱。

代码如下:

package com.yzh7.test1;

/**
 * @author: hy
 * @create: 2022-06-28 09:19:30
 */
public class Student {
//         1.创建学生类
//        包括:
//        属性:姓名,年龄,电话,爱好
//        方法1:自我介绍(无参,无返回值)
//        输出姓名,年龄,电话,爱好
//        方法2:上网(有参,无返回值)
//        参数:上网小时,每小时多少钱
//        输出:上网多长时间,花了多少钱。
    //属性
    //姓名
    String name;
    //年龄
    int age;
    //电话
    String phone;
    //爱好
    String hobby;

    //构造
    public Student(){}

    //有参构造
    public Student(String name,int age,String phone,String hobby){
        this.name=name;
        this.age=age;
        this.phone=phone;
        this.hobby=hobby;
    }

    //方法
    //自我介绍
    public void introduce(){
        System.out.println(this.name+" "+this.age+" "+this.phone+" "+this.hobby);
    }
    //上网
    public void goOnline(int hours,double price){
        System.out.println(this.name+"上网"+hours+"小时,每小时"+price+"元");
        double money =  price*hours;
        System.out.println("一共花了:"+money+"元");
    }
}
package com.yzh7.test1;

/**
 * @author: hy
 * @create: 2022-06-28 09:24:08
 */
public class Test {
    public static void main(String[] args) {
//        2.使用学生类,创建两个学生对象,
//        并调用自我介绍方法,输出学生信息
//        调用上网方法,输出上网信息
        //创建学生对象
        Student s1 = new Student();
        s1.name="张三";
        s1.age=20;
        s1.hobby="打球";
        s1.phone="13112345678";
        //调用方法
        s1.introduce();
        s1.goOnline(10,5);

        //用有参构造创建学生对象
        Student s2 = new Student("李四",18,"1321123425","唱歌");
        //调用方法
        s2.introduce();
        s2.goOnline(5,4);
    }
}

运行结果如下:
#pic_center

2.手机属性方法

代码如下:

package com.yzh7.test2;

/**
 * @author: hy
 * @create: 2022-06-28 09:37:56
 */
public class Phone {
    //属性
    String brand;
    //颜色
    String color;
    //价格
    int price;

    //有参构造
    public Phone(String brand,String color,int price){
        this.brand=brand;
        this.color=color;
        this.price=price;
    }

    //方法
    //给手机充电:充多少个小时。有参,有返回值
    //如果没有充够2小时,返回没充满。如果充够2小时,返回充满了
    public String charge(int hours){
        System.out.println("为手机充电"+hours+"小时");
        if(hours<2){
            return "没充满";
        }

        if(hours<10){
            return "充满了";
        }

        return "充爆了";
    }

    //拿手机打电话:给谁,打多长时间,每分钟多少钱。有参有返回值
    //输出打电话的信息,返回花了多少钱
    public double call(String target,int min,double price){
        System.out.println("用"+this.brand+"手机给"+target+"打电话");
        System.out.println("打了"+min+"分钟,每分钟"+price+"元");
        double money = price*min;
        return money;
    }

}
package com.yzh7.test2;

/**
 * @author: hy
 * @create: 2022-06-28 09:45:33
 */
public class Test {
    public static void main(String[] args) {
        //因为类中只有有参构造,没有无参构造,所以必须传参
        Phone p = new Phone("小米","黑色",2000);
        //方法有返回值,就要定义变量接收返回值
        String res = p.charge(3);
        System.out.println(res);
        double money =  p.call("李四",20,0.5);
        System.out.println("一共花了"+money+"元");

        //定义汽车类:
        // 属性:牌子,价格,时速
        // 方法:加油(升,价格) 为汽车加油,多少升,每升多少钱,返回加油花了多少钱
        // 方法:跑() 无参,无返回值。要求根据时速判断,如果时速小于100,输出安全驾驶,如果大于100,输出危险驾驶

        //定义计算器类:
        //方法1:实现求任意两个数字区间的和
        //方法2:返回1-100的随机数
        System.out.println(Math.random());
    }
}

运行结果如下:
#pic_center

3.求两个数字区间和 返回1-100的随机数

代码如下:

package com.yzh7.test3;

/**
 * @author: hy
 * @create: 2022-06-28 10:12:38
 */
public class Calc {
    //求两个数字区间和
    public void sum(int start,int end){
        int he=0;
        for(int i=start;i<=end;i++){
            he+=i;
        }
        System.out.println(he);
    }
    //返回1-100的随机数
    public int getNum(){
        // [0,1) * 100 [0,100) 0-99.99999 +1   1-100.99999
       return (int)(Math.random()*100+1);
    }
}
package com.yzh7.test3;

/**
 * @author: hy
 * @create: 2022-06-28 10:15:09
 */
public class Test {
    public static void main(String[] args) {
        Calc c = new Calc();
        c.sum(1,100);
        System.out.println(c.getNum());
    }
}

运行结果如下:
#pic_center

4.加法

代码如下:

package com.yzh7.test4;

/**
 * @author: hy
 * @create: 2022-06-28 10:35:16
 */
public class JiaFa {
    public void add(int n1,int n2){
        System.out.println(n1+n2);
    }

    double add(int x,double y){
        System.out.println(x+y);
        return x+y;
    }

    double add(double x,int y){
        System.out.println(x+y);
        return x+y;
    }


    int add(int x,int y,int z){
        System.out.println(x+y);
        return x+y;
    }

    public void add(double x,double y){
        System.out.println(x+y);
    }

    public void add(String s1,String s2){
        System.out.println(s1+s2);
    }
}
package com.yzh7.test4;

import java.util.Date;

/**
 * @author: hy
 * @create: 2022-06-28 10:28:43
 */
public class Test {
    public static void main(String[] args) {
        //方法重载:方法名相同,参数不同,构成了方法重载.
        //好处: 方便调用
        //特征:方法重载只与方法名和参数有关,方法名相同,参数不同(个数,类型,次序)
        //    与参数名,返回值,访问修饰符无关。

        /*System.out.println(100);
        System.out.println("abc");
        System.out.println(true);
        System.out.println(new Date());
        System.out.println(1.234);
        System.out.println(Math.max(100,200));
        System.out.println(Math.max(1.234,4.5234));*/

        //自己定义重载方法
        JiaFa jf = new JiaFa();
        jf.add(1,2);
        jf.add(1,2,3);
        jf.add(2.34,23);
        jf.add("aa","bb");
        jf.add("cc","dd");
    }
}

运行结果如下:
#pic_center

5.跨包调用同名不同包的类

代码的路径如下如所示:
在这里插入图片描述

代码如下:

package com.yzh7.test5.ali;

/**
 * @author: hy
 * @create: 2022-06-28 10:57:20
 */
public class FaceRec {
    public void work(){
        System.out.println("使用ali的人脸识别功能....");
    }
}

package com.yzh7.test5.baidu;

/**
 * @author: hy
 * @create: 2022-06-28 10:57:26
 */
public class FaceRec {
    public void work(){
        System.out.println("使用百度的人脸识别....");
    }
}

//声明包
package com.yzh7.test5.test;

//导入包
//import com.yzh7.test5.ali.FaceRec;
//import com.yzh7.test5.baidu.FaceRec;

/**
 * @author: hy
 * @create: 2022-06-28 10:58:17
 */
public class Test {
    public static void main(String[] args) {
        //alt+enter 修正错误,自动提示修复建议。提示import class

        //带包命,打完整的类
        com.yzh7.test5.ali.FaceRec fr = new com.yzh7.test5.ali.FaceRec();
        fr.work();

        com.yzh7.test5.baidu.FaceRec fr2 = new  com.yzh7.test5.baidu.FaceRec();
        fr2.work();
    }
}

运行结果如下:
#pic_center

6.权限修饰符 public protected 默认 private

代码的路径如下如所示:
在这里插入图片描述

代码如下:

package com.yzh7.test6.aaa;

import com.yzh7.test6.MyCls;

/**
 * @author: hy
 * @create: 2022-06-28 11:10:30
 */
public class Demo {
    public static void main(String[] args) {
        MyCls myCls = new MyCls();
        System.out.println(myCls.num);
    }
}
package com.yzh7.test6.aaa;

import com.yzh7.test6.MyCls;

/**
 * @author: hy
 * @create: 2022-06-28 11:13:03
 */
public class SubCls extends MyCls {
    public void xxx(){
        System.out.println(num);
    }
}
package com.yzh7.test6;

/**
 * @author: hy
 * @create: 2022-06-28 11:05:15
 */
public class MyCls {
    //定义属性
    public int num;

    public void test(){
        System.out.println(num);
    }
}
package com.yzh7.test6;

/**
 * @author: hy
 * @create: 2022-06-28 11:07:13
 */
public class Test {
    public static void main(String[] args) {
        //private: 私有修饰符 在定义该属性/方法的外部不能访问。只能在类内部访问。
        //默认级别(不写修饰符):在同包下的类可以访问,不同包的类不能访问。
        //protect(受保护的修饰符):同包下的类可以访问。不同包的子类可以访问。不同包的非子类不能访问。
        //public(公有修饰符):在同包下的类可以访问,不同包的类也可以访问

        MyCls myCls = new MyCls();
        System.out.println(myCls.num);
        myCls.test();
    }
}

运行结果如下:
#pic_center

7.static 静态变量/方法

代码如下:

package com.yzh7.test7;

/**
 * @author: hy
 * @create: 2022-06-28 11:38:14
 */
public class Student {
    String name;
    int age;
    //静态变量被共享
    static int x = 100;

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

    public void introduce(){
        System.out.println(this.name+" "+this.age+" "+x);
    }

}
package com.yzh7.test7;

/**
 * @author: hy
 * @create: 2022-06-28 11:31:51
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(x);
        System.out.println(Test.x);
        //System.out.println(num);
        //System.out.println(Test.num);
        Test t = new Test();
        System.out.println(t.num);

        //通过类直接调用静态方法
        Test.ccc();

    }

    //成员变量/全局变量/实例变量
    int num;
    static int x;

    //普通方法/成员方法/实例方法
    public void aaa(){
        System.out.println(num);
        bbb();
        System.out.println(x);
        ccc();
    }

    public void bbb(){

    }

    public static void ccc(){
        //System.out.println(num);
        //bbb();

        System.out.println(x);
        ddd();
    }

    public static void ddd(){

    }
}
package com.yzh7.test7;

/**
 * @author: hy
 * @create: 2022-06-28 11:37:23
 */
public class Test2 {
    public static void main(String[] args) {
        //static 静态
        //静态属性/静态方法

        //静态属性:直接与类关联,内存中只有一份,被所有该类的对象共享

        //静态变量直接通过类名访问,与实例没有关系。被实例对象共享
        Student.x=500;

        Student s1 = new Student("张三",20);
        Student s2 = new Student("李四",18);
        s1.introduce();
        s2.introduce();

        System.out.println("========================");
        //修改s1的姓名为张三丰
        s1.name="张三丰";
        //通过s1将静态变量x改为200
        s1.x=200;

        s1.introduce();
        s2.introduce();

    }
}

运行结果如下:
#pic_center

8.静态代码块/实例代码块

代码如下:

package com.yzh7.test8;

/**
 * @author: hy
 * @create: 2022-06-28 11:55:54
 */
public class MyCls {
    //实例代码块:每次创建对象的时候,都会在构造方法之前执行一次
    {
        System.out.println("实例代码块被执行");
    }

    //只在加载类的时候,执行一次
    static {
        System.out.println("静态代码块被执行");
    }

    public MyCls(){
        System.out.println("构造方法被执行");
    }
}
package com.yzh7.test8;

/**
 * @author: hy
 * @create: 2022-06-28 11:56:40
 */
public class Test2 {
    public static void main(String[] args) {
        new MyCls();
        new MyCls();
    }
}

package com.yzh7.test8;

/**
 * @author: hy
 * @create: 2022-06-28 11:49:39
 */
public class Saler {
    //姓名
    String name;
    //票数
    static int ticket=10;

    public void sale(){
        //Saler.ticket
        if(this.ticket>0){
            this.ticket--;
            System.out.println(this.name+"卖票一张,剩余:"+this.ticket);
        }else{
            System.out.println(this.name+"票已卖完");
        }
    }
}
package com.yzh7.test8;

/**
 * @author: hy
 * @create: 2022-06-28 11:51:18
 */
public class Test {
    public static void main(String[] args) {
        Saler s1 = new Saler();
        s1.name="孙悟空";
        Saler s2 = new Saler();
        s2.name="白骨精";

        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
        s1.sale();
        s2.sale();
    }
}

运行结果如下:
#pic_center

9.截取/首字母大写/回文数

代码如下:

package com.yzh7.test;

/**
 * @author: hy
 * @create: 2022-06-29 09:55:20
 */
public class MyUtil {
    //将传入的字符串,转为首字母大写,然后返回
    public static String convertUpper(String str){
        //left
        //String.valueOf():将其他类型转为字符串类型的数据
        String firstLetter =String.valueOf(str.charAt(0)).toUpperCase(); //将传入内容的首字母转为大写
        String otherLetter = str.substring(1); //获取传入字符串的剩余部分
        return firstLetter+otherLetter;
    }
}
package com.yzh7.test;

/**
 * @author: hy
 * @create: 2022-06-29 09:40:48
 */
public class Test {
    public static void main(String[] args) {
        //String s1 = "abc";
        //String s2 = new String("abc");

        //System.out.println(s1);
        //System.out.println(s2);

        //System.out.println(s1.length());
        //System.out.println(s2.length());

        //charAt返回指定位置的字符
        //System.out.println(s1.charAt(1));
        //查找指定字符串在主字符串中的位置,找不到返回-1
        //System.out.println(s1.indexOf("ac"));

        //substring(开始位置):从主字符串的指定位置直接截取到最后,返回截取的字符串
        //substring(开始位置,结束位置):从主字符串的开始位置截取到结束位置,不包含结束位置,返回截取的字符串
        String s = "花果山水帘洞齐天大圣孙悟空";
        //题目:请截取齐天大圣孙悟空
        //int pos = s.indexOf("齐");
        //System.out.println(pos);
        //String res = s.substring(pos);
        //System.out.println(res);

        //截取齐天大圣
        /*int start = s.indexOf("齐");
        int end = s.indexOf("圣")+1;
        String res = s.substring(start,end);
        System.out.println(res);*/

        //System.out.println(s.startsWith("2花果山"));
        //replace("旧","新")
        //System.out.println(s.replace("孙悟空","猪八戒"));


        //把css的样式,换成js的样式写法
        //border-left-width:1   borderLeftWith:1
        String css ="background-color"; //"border-left-width";
        System.out.println(css);
        //String res = css.replace("-","");
        //System.out.println(res);
        //split("分隔符"):使用指定分隔符拆分字符串,返回字符串数组
         String[] strs =  css.split("-");
         String res = "";
         for(int i=0;i<strs.length;i++){
             //System.out.println(strs[i]);
             //除了第一个单词不变,其他单词要变大写
            if(i==0){
                //如果第一个单词,则直接拼接
                res+=strs[i];
            }else{
                //如果是后面的单词,则先转大写,再拼接
                res+=MyUtil.convertUpper(strs[i]);
            }
         }

        System.out.println(res);

         //截取身份证号的年月日
    }
}
package com.yzh7.test;

/**
 * @author: hy
 * @create: 2022-06-29 10:33:04
 */
public class Test2 {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";
        String str3 = new String("abc");

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("=================");
        // == : 比较的变量中存储的数据的地址
        // equals:比较的是变量中存储的数据的内容
        System.out.println(str1==str2);
        System.out.println(str1==str3);
        System.out.println("==================");
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));

    }
}
package com.yzh7.test;

/**
 * @author: hy
 * @create: 2022-06-29 10:50:06
 */
public class Test3 {
    public static void main(String[] args) {
        //字符串得不可变性
        /*String s = "abc";
        s+="123";
        s+="qwe";
        System.out.println(s);*/
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("qwe");
        stringBuilder.append("123");
        stringBuilder.insert(1,"456"); //在1的位置插入字符串
        stringBuilder.reverse(); //反转字符串
        //System.out.println(stringBuilder);
        //toString():将目标类型转为字符串数据
        //String s = stringBuilder.toString();
        //System.out.println(s);

        //输出100-999之间的回文数
        // 111 121 131 202 212.... 252
        for(int i=100;i<=999;i++){
            StringBuilder str = new StringBuilder();
            //在StringBuilder中添加内容
            str.append(i);
            String str1 = str.toString();
            String str2 = str.reverse().toString();
            //System.out.println(str1);
            //System.out.println(str2);
            if(str1.equals(str2)){
                System.out.println(i);
            }
        }

        //测试StringBuffer
         for (int i = 100; i <= 999; i++) {
            java.lang.StringBuffer str=new java.lang.StringBuffer();
            //添加i的值
            str.append(i);
            String str1=str.toString();
            String str2=str.reverse().toString();
            if(str1.equals(str2)){
                System.out.println(i);
            }
        }

    }
}

运行结果如下:
#pic_center

10.日期格式化类 StringBuilder和StringBuffer

代码如下:

package com.yzh7.test2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: hy
 * @create: 2022-06-29 11:20:43
 */
public class Test {
    public static void main(String[] args) throws ParseException {
        Date d = new Date();
        System.out.println(d);
        //日期格式化类
        //2022-01-01 01/01/2022  2022年01月01日
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        //format:将日期类型额数据转为特定格式的文本
        //String res = sdf.format(d);
        //System.out.println(res);

        //求两个日期差了多少天
        String str = "2010-03-05";
        String str2 = "2019-04-25";
        System.out.println(str);
        System.out.println(str2);
        //将字符串日期转为日期对象
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        //将字符串转为日期类型
        Date d1 = sdf2.parse(str);
        Date d2 = sdf2.parse(str2);
        System.out.println(d1);
        System.out.println(d2);
        System.out.println("==================");
        //getTime():返回日期对象相对于1970-01-01的毫秒数
        //System.out.println(d1.getTime()/1000/3600/24/365);
        //System.out.println(d2.getTime()/1000/3600/24/365);
        //计算两个日期的毫秒数的差,从而计算出天数差
        System.out.println((d2.getTime()-d1.getTime())/1000/3600/24);

        //日历类
        //Calendar


        //日期转字符串,字符串转日期
        //封装工具类:求两个字符串的日期差(年,月,天数)
    }
}
package com.yzh7.test2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * @author: hy
 * @create: 2022-06-29 11:55:42
 */
public class Test2 {
    public static void main(String[] args) {
        //jdk1.8之后提供的日期相关类,与之前的Date,SimpleDateFormat的用法差别非常大
        //获取当前日期时间的年月日,时分秒对象
        LocalDateTime ldt = LocalDateTime.now();
        //2022-06-29T11:57:38.910
        System.out.println(ldt.toString());

        //获取当前日期时间的年月日对象
        //2022-06-29
        LocalDate ld = LocalDate.now();
        System.out.println(ld);

        //获取当前日期时间的时间对象
        //11:58:51.683
        LocalTime lt = LocalTime.now();
        System.out.println(lt);
        System.out.println("========================");
        //日期格式化对象(SimpleDateFormat)
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        //调用日期对象的格式化方法
        String res =  ld.format(dtf);
        System.out.println(res);
        System.out.println("============================");
        String s1 = "2010-01-01";
        String s2 = "2019-09-08";
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        //转年月日时分秒时,中间用T间隔
        //LocalDateTime.parse("2022-01-01T12:12:12");

        LocalDate ld1 =  LocalDate.parse(s1,dtf2);
        LocalDate ld2 = LocalDate.parse(s2,dtf2);
        System.out.println(ld1);
        System.out.println(ld2);

        System.out.println("=======================");
        //Duration:计算以时分秒为单位的日期差,如果日期没有包含时分,直接报错
        //Period:计算以年月日为单位的日期差
        Period period = Period.between(ld1,ld2);
        //计算年月日差的时候,默认没有考虑其他部分的时间
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

    }
}

运行结果如下:
#pic_center

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值