加载中...

这些Java语法易错点你记住多少?(本文适用于Java初学者)

本文说明

Java基础语法学过一遍后,发现很多基础理论不是很清楚,根据自身的需要,查阅了各种资料论坛与博客,经过本人的整理与总结,写了这一篇易错基础回顾的博客。文中素材和图片部分借鉴优质博客与论坛。

一.涉及内容概述

1.1 Java编程的基本结构
1.2 Scanner怎么进行输入?
1.3 Break和Continue的区别是什么?
1.4 带标签的Continue写法
1.5 测试Class的调用
1.6 测试Constctor构造器
1.7 测试Method方法
1.8 测试Object对象
1.9 测试Overload重载
2.0 测试Override重写
2.1 测试Extends继承
2.2 测试Interface接口
2.3 测试抽象类
2.4 测试内部类
2.5 如何使用Foreach循环?
2.6 测试递归和迭代效率测试

二.SHOW YOUR CODE

1.1 Java编程的基本结构

public class TestJava {
        public static void main(String[] args) {
        
        }
}

1.2 Scanner怎么进行输入?

import java.util.Scanner;

public class TestScanner {
        public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                String name = scanner.nextLine();
                int age = scanner.nextInt();
	
                System.out.println("My name is:"+name);
                System.out.println("My age is:"+age);	
        }	
}

1.3 Break和Continue的区别是什么?

Break的测试如下:

public class TestBreak {
        public static void main(String[] args) {	
                //定义计时器
                int total = 0;
                //生成随机数
                while(true) {
                        total++;	//每循环一次加一
                        int i = (int)Math.round(100*Math.random());
                        System.out.println(i);
                        if(i == 98) {
                            break;
                        }
                }
                //输出循环的次数
                System.out.println("Game is over,used "+total+" time");
        }

}

Continue的测试如下:

public class TestContinue {
        public static void main(String[] args) {
                //记录total
                int total = 0;
                System.out.println("Begin");
                        total++;
                        for(int i=0;i<10;i++) {
                                if(i%2==0) {
                                        continue;
                                }
                                System.out.println(i);
                        }
        }
}

小结:Break可用于跳出循环,Continue用于结束本次循环,继续下一循环

1.4 带标签的Continue写法

public class TestLabelContinue {
        public static void main(String[] args) {
                //打印100~150之间的质数
                outer:for(int i=100;i<=150;i++) {
                        for(int j=2;j<i/2;j++) {
                                if(i%j==0) {
                                    continue outer;
                                }
                        }
                        System.out.println(i+" ");
                }
        }
}

1.5 测试Class的调用

 public class TestClass {

        //属性fields
        int id;
        String sname;
        int age;

        Computer comp;	//计算机

        //方法
        void study() {
                System.out.println("我在学习"+comp.brand);
        }

        void play() {
                System.out.println("我在打游戏");
        }

        public static void main(String[] args) {
                TestClass stu = new TestClass();
                stu.id = 20201528;
                stu.sname = "eason";
                stu.age =18;
                
                Computer c1 = new Computer();
                c1.brand = "联想";
                stu.comp = c1;
	
                stu.play();
                stu.study();
        }
}

class Computer {
    String brand;	//品牌
 }

1.6 测试Constctor构造器

class Point {
        double x, y;

        //构造方法名称和类名一致
        public Point(double _x, double _y) {
                x = _x;
                y = _y;
        }
        //算距离
        public double getDistance(Point p) {
                return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
        }
}

public class TestConstctor {

        public static void main(String[] args) {
                Point p = new Point(3.0, 4.0);
                Point origin =new Point(0.0, 0.0);
	
                System.out.println(p.getDistance(origin));
        }

}

1.7 测试Method方法

public class TestMethod {
        //通过对象调用普通方法
        public static void main(String[] args) {
                TestMethod tm = new TestMethod();
                tm.printDate();
                int d = tm.add(1, 2, 3);
                System.out.println(d);
        }

        void printDate() {
                System.out.println("今年18岁");
        }

        int add(int a,int b,int c) {
                return a+b+c;
        }
}

1.8 测试Object对象

public class TestObject {

        public static void main() {
	
                TestObject to = new TestObject();
                System.out.println(to.toString());
	
        }

        public String toString() {
                return "测试Object对象";
        }
}

1.9 测试Overload重载

public class TestOverload {

        public static void main(String[] args) {
	
        }

        public static void add(int a, int b) {
                System.out.println(a+b);
        }

        //不构成重载
        /*
         //仅有形参不同
         public static void add(int b, int a){
                System.out.println(a+b);
         }
         //仅有返回值类型不同
         public static int add(int a, int b){
         }
  
         */

}

2.0 测试Override重写

public class TestOverride {

        public static void main(String[] args) {
                Car car = new Car();
	
                car.run();
                car.stop();
        }
}
//Vehicle
class Vehicle {
        public void run() {
                System.out.println("跑");
        }
        public void stop() {
                System.out.println("停下!");
        }
        public Person Whoispsg() {
                return new Person();
        }
}
//Car
class Car extends Vehicle {
        public void run() {
                System.out.println("开车");
        }
        public Student Whoispsg() {
                return new Student();
        }
}

2.1 测试Extends继承

public class TestExtends {
    
        public static void main(String[] args) {
                Student stu = new Student();
                stu.name = "eason";
                stu.height = 164;
                stu.major = "挖掘机";
	
                stu.rest();
                stu.study();
	
                System.out.println(stu instanceof Person);
                System.out.println(stu instanceof Object);
        }
}

class Person /*extends Object*/{
        String name;
        int height;

        public void rest() {
                System.out.println("休息一下");
        }
}

class Student extends Person {
        String major;

        public void study() {
                System.out.println("学习学习");
        }
}

2.2 测试Interface接口

public class TestInterface {

        public static void main(String[] args) {
                Volant v = new Angel();
                v.fly();
                Honest h1 = new GoodMan();
                h1.helpOther();
                Honest h2 = new BadMan();
                h2.helpOther();
        }

}	

         /**
         * 飞行接口
         * @author RealH
         *
         */
interface Volant {
        int FLY_HEIGHT = 1000;
        void fly();
}

//善良接口
interface Honest {
        void helpOther();
}
//Angel
class Angel implements Volant,Honest {//实现类可以实现多个父接口

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("Angel.helpOther();");
        }

        @Override
        public void fly() {
                // TODO Auto-generated method stub
                System.out.println("Angel.fly();");
        } 

}
//GoodMan
class GoodMan implements Honest {

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("GoodMan.helpOther();");
        }

}
//BadMan
class BadMan implements Honest {

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("BadMan.helpOther();");
        }

}

2.3 测试抽象类

public abstract class Animal {
        //没有实现,但子类必须有实现,子类必须重写,抽象类不能new,只能用来继承
        abstract public void shout();

        public void run() {
                System.out.println("跑");
        }

        public static void main(String[] args) {
                Animal a = new Dog();

                a.shout();
        }

}

class Dog extends Animal {
        @Override
        public void shout() {
                System.out.println("汪!");
        }
}

2.4 测试内部类

public class TestInnerClass {
        public static void main(String[] args) {

                //创建内部类对象
                Outer.Inner inner = new Outer().new Inner();
                inner.show();
        }
}

class Outer {
        private int age = 10;

                class Inner {
                        int age = 20;
                        public void show() {
                                int age = 30;
                                System.out.println("外部类的成员变量age:"+Outer.this.age);
                                System.out.println("内部类的成员变量age:"+this.age);
                                System.out.println("局部变量age:"+age);
                        }
                }
}

2.5 如何使用Foreach循环?

public class TestForeach {

        public static void main(String[] args) {
                int[] a = new int[4];
                System.out.println(a.length);
                //初始化
                for(int i = 0; i < a.length; i++) {
                        a[i] = 100*i;
                }

                //读取元素
                //for(int j = 0; j <= a.length; j++) {
                //	System.out.println(a[j]);
                //}
                System.out.println("#########");
                //foreach循环来读取数组元素
                for(int m : a) {
                        System.out.println(m);
                }

                String[] ss = {"aa", "bb", "cc", "dd"};
                for(String temp : ss) {
                        System.out.println(temp);
                }
        }
}

2.6 测试递归和迭代效率测试

public class TestRecursion {

        public static void main(String[] args) {
                long d1 = System.currentTimeMillis();	//返回当时的时刻毫秒数ms
                System.out.printf("%d的阶乘结果为:%s\n", 10, factorial(10));
                long d2 = System.currentTimeMillis();
                System.out.printf("递归的耗时为:%s\n", d2-d1);
        }
        //递归必须有递归头和递归体
        static long factorial(int n) {
                if(n == 1) {	//递归头
                        return 1;
                } else {		//递归体
                        return n*factorial(n-1);
                }
        }

}
posted @ 2021-07-20 05:59  nongeason  阅读(127)  评论(0)    收藏  举报