zoukankan      html  css  js  c++  java
  • 2020-08-05日报博客

    2020-08-05日报博客

    1.完成的事情:

    • 完成CodeGym Java基础level 14部分,并完成练习题

    2.遇到的问题:

    • 对象类型的转换,子类是一个父类,is-a。

    3.明日计划:

    • 继续学习Java。
    /*taskKey="zh.codegym.task.task14.task1421"
    
    单例
    
    如果某个类只允许创建其类型的一个对象,则为单例。
    实现单例模式:
    1.在单独的文件中创建 Singleton 类。
    2.在类中添加 static getInstance() 方法。
    3.随时调用 getInstance 方法,getInstance 方法必须返回相同的 Singleton 对象。
    4.考虑如何防止创建此类的其他实例。
    5.让 Singleton 类的所有构造方法设为 private。
    6.因此,它应只能使用 getInstance 方法创建一个对象(该类的实例)。
    
    
    Requirements:
    1.	Singleton 类必须位于单独的文件中。
    2.	Singleton 类应防止在类外部创建 Singleton 对象。
    3.	Singleton 类必须包含 private static Singleton 字段 instance。
    4.	Singleton 类必须实现 public static getInstance 方法,该方法返回字段 instance 的值。
    5.	getInstance 方法必须始终返回相同对象。*/
    
    package zh.codegym.task.task14.task1421;
    
    public class Singleton {
        private static Singleton instance = new Singleton();
    
        private Singleton() {
        }
    
        public static Singleton getInstance(){
            return instance;
        }
    }
    
    package zh.codegym.task.task14.task1421;
    
    /* 
    单例
    */
    public class Solution {
        public static void main(String[] args) {
        }
    }
    
    /*taskKey="zh.codegym.task.task14.task1420"
    
    最大公约数 (GCD)
    
    最大公约数 (GCD)。
    从键盘输入 2 个正整数。
    显示最大公约数。
    
    
    Requirements:
    1.	程序应从键盘读取 2 行内容。
    2.	如果输入的行不能转换为正整数,则会抛出异常。
    3.	程序应在屏幕上显示数据。
    4.	程序应显示从键盘读取的数字的最大公约数 (GCD),然后成功终止。*/
    
    package zh.codegym.task.task14.task1420;
    
    /* 
    最大公约数 (GCD)
    */
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Solution {
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            int a,b;
            try{
                a = Integer.parseInt(reader.readLine());
                b = Integer.parseInt(reader.readLine());
                if(a > 0 && b > 0){
                    findGCD(a,b);
                }else{
                    throw new Exception();
                }
            }catch(Exception e){
                throw  e;
            }
    
        }
        public static int findGCD(int a,int b){
            int result = 0;
            for (int i = 1; i <= a && i <= b; i++){
                if(a % i ==0 && b % i ==0){
                    result = i;
                }
            }
            System.out.println(result);
            return result;
        }
    }
    
    /*taskKey="zh.codegym.task.task14.task1419"
    
    引入异常
    
    在列表 exceptions 中填充十个 (10) 不同的异常。
    第一个异常已在 initExceptions 方法中实现。
    
    
    Requirements:
    1.	exceptions 列表必须包含 10 个元素。
    2.	exceptions 列表中的所有项目都必须是异常(Throwable 类的后代)。
    3.	exceptions 列表中的所有项目都必须唯一。
    4.	initExceptions 方法必须为 static。*/
    
    
    package zh.codegym.task.task14.task1419;
    
    import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
    
    import java.lang.annotation.AnnotationTypeMismatchException;
    import java.nio.BufferOverflowException;
    import java.nio.BufferUnderflowException;
    import java.nio.channels.AcceptPendingException;
    import java.nio.channels.AlreadyBoundException;
    import java.security.DigestException;
    import java.security.acl.AclNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.MissingResourceException;
    
    /* 
    引入异常
    */
    
    public class Solution {
    
        public static List<Exception> exceptions = new ArrayList<>();
    
        public static void main(String[] args) {
            initExceptions();
    
            for (Exception exception : exceptions) {
                System.out.println(exception);
            }
        }
    
        private static void initExceptions() {   // 第一个异常
            try {
                float i = 1 / 0;
    
            } catch (Exception e) {
                exceptions.add(e);
            }
    
            exceptions.add(new CloneNotSupportedException());
            exceptions.add(new DigestException());
            exceptions.add(new ArrayIndexOutOfBoundsException());
            exceptions.add(new ArrayStoreException());
            exceptions.add(new ClassCastException());
            exceptions.add(new AclNotFoundException());
            exceptions.add(new AcceptPendingException());
            exceptions.add(new AlreadyBoundException());
            exceptions.add(new BufferOverflowException());
    
        }
    }
    
    
  • 相关阅读:
    MySQL8.0.x免安装配置
    Java中锁的实现与内存语义
    并发中的volatile
    「LOJ #2163」「POI2011」Tree Rotations
    「CodeChef REBXOR」Nikitosh and xor
    「Codeforces 429D」Destiny
    「Luogu P2042」「NOI2005」维护数列
    「SPOJ SEQ」 Recursive Sequence
    「GCJ 2008 Round 1A C」numbers
    「LOJ #6016」崂山白花蛇草水
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/13451069.html
Copyright © 2011-2022 走看看