吐血共享我的java学习代码笔记.doc_第1页
吐血共享我的java学习代码笔记.doc_第2页
吐血共享我的java学习代码笔记.doc_第3页
吐血共享我的java学习代码笔记.doc_第4页
吐血共享我的java学习代码笔记.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

int max = Integer.MAX_VALUE ;/ 求出最大值System.out.println(整型的最大值: + max) ;char ch1 = a ;/ 字符是使用”“括起来的数据char ch2 = 97 ;/ 通过数字定义字符变量char ch1 = ;/ 表示的是一个char ch2 = ;/ 表示的是一个、float num = 3.0f ;/ 定义一个浮点型变量boolean flag = true ;/ 定义布尔型变量System.out.println(flag = + flag) ;/ 打印输出String str = lixinghua ;/ 定义字符串变量int x = 30 ;str = str + x ;/ 修改str的内容并将内容重新给str变量public static void main(String args)for(int i=0;iy?x:y ;/ 通过三目运算符求出最大值int score = null ;/ 声明数组score = new int3 ;/ 为数组开辟空间,大小为3System.out.println(数组长度为: + score.length) ;int score = 91,92,93,94,95,96 ;/ 使用静态初始化声明数组for(int x=0;xscore.length;x+)/ 循环输出System.out.println(score+x+ = + scorex) ;public static void main(String args)int score = 67,89,87,69,90,100,75,90 ;/ 使用静态初始化声明数组int max = 0 ;/ 保存数组中的最大值int min = 0 ;/ 保存数组中的最小值max=min=score0;for(int x=0;xmax)max=scorex;else if(scorexmin)min=scorex;System.out.println(最高成绩: + max) ;System.out.println(最低成绩: + min) ;public static void main(String args)int score = 67,89,87,69,90,100,75,90 ;/ 使用静态初始化声明数组for(int i=1;iscore.length;i+)for(int j=0;jscore.length;j+)if(scoreiscorej)/ 交换位置int temp = scorei ;/ 中间变量scorei = scorej ;scorej = temp ;for(int i=0;iscore.length;i+)/ 循环输出System.out.print(scorei+t) ;public static void main(String args)int score = new int43 ;/ 声明并实例化二维数组score01 = 30 ;/ 为数组中的内容赋值score10 = 31 ;/ 为数组中的内容赋值score22 = 32 ;/ 为数组中的内容赋值score31 = 33 ;/ 为数组中的内容赋值score11 = 30 ;/ 为数组中的内容赋值for(int i=0;iscore.length;i+)for(int j=0;jscorei.length;j+)System.out.print(scoreij + t) ;System.out.println() ;public static void main(String args)int score = 67,61,78,89,83,99,100,98,66,95 ;/ 静态初始化完成,每行的数组元素个数不一样1for(int i=0;iscore.length;i+)for(int j=0;jscorei.length;j+)System.out.print(scoreij + t) ;System.out.println() ;public static void main(String args)int score = 5,1 ,6,7,9,4,8,3 ;/ 静态初始化完成,每行的数组元素个数不一样1for(int i=0;iscore.length;i+)for(int j=0;jscorei.length;j+)for(int k=0;kscoreij.length;k+)System.out.println(score+i+j+k+ = +scoreijk + t) ;public static void main(String args)PrintInto();System.out.print(您好!);public static void PrintInto()char c=h,e,l,o;for(int x=0;xc.length;x+)System.out.print(cx);System.out.println();public static void main(String args)System.out.println(计算结果: + sum(100) ;/ 调用操作public static int sum(int num)/ 定义方法用于求和操作if(num=1)/ 判断是否是加到了最后一个数return 1 ;elsereturn num + sum(num-1) ;/ 递归调用public static void main(String args)System.out.print(不传递参数(fun()):) ;fun() ;/ 不传递参数System.out.print(n传递一个参数(fun(1)):) ;fun(1) ;/ 传递一个参数System.out.print(n传递五个参数(fun(1,2,3,4,5)):) ;fun(1,2,3,4,5) ;public static void fun(int . arg)/ 可变参数for(int i=0;i + (str1=str2) ;/ falseSystem.out.println(str1 = str3 - + (str1=str3) ;/ falseSystem.out.println(str2 = str3 - + (str2=str3) ;/ trueSystem.out.println(str1 equals str2 - + (str1.equals(str2) ;/ trueSystem.out.println(str1 equals str3 - + (str1.equals(str3) ;/ trueSystem.out.println(str2 equals str3 - + (str2.equals(str3) ;/ truestatic String country = A城 ;/ 定义城市属性,有默认值,staticp1.country = B城 ;/ 修改static属性public static void setCountry(String c) / 此方法可以直接由类名称调用country = c ;Person.setCountry(B城) ;/ 调用静态方法修改static属性的内容class Person/ 定义Person类private static String country = A城 ; / 定义静态属性private String name = Hello ;public static void sFun(String c)System.out.println(name = + name) ;/ 错误,不能调用非static属性fun() ;/ 错误,不能调用非static方法public void fun()System.out.println(World) ;class Demo/ 直接在类中编写代码块,称为构造块System.out.println(1、构造块。) ;static/ 使用static,称为静态代码块System.out.println(0、静态代码块) ;public Demo()/ 定义构造方法System.out.println(2、构造方法。) ;public class textstatic/ 在主方法所在的类中定义静态块System.out.println(在主方法所在类中定义的代码块) ;public static void main(String args)new Demo() ;/ 实例化对象new Demo() ;/ 实例化对象new Demo() ;/ 实例化对象;结果:0、静态代码块1、构造块。2、构造方法。1、构造块。2、构造方法。1、构造块。2、构造方法。class Singletonprivate Singleton()/ 将构造方法进行了封装,私有化public void print()System.out.println(Hello World!) ;public class SingletonDemo02public static void main(String args)Singleton s1 = null ;/ 声明对象s1 = new Singleton() ;/ 错误,无法实例化对象;class Singletonprivate static Singleton instance = new Singleton() ;/ 在内部产生本类的实例化对象public static Singleton getInstance()/ 通过静态方法取得instance对象return instance ;private Singleton()/ 将构造方法进行了封装,私有化public void print()System.out.println(Hello World!) ;public class SingletonDemo05public static void main(String args)Singleton s1 = null ;/ 声明对象Singleton s2 = null ;/ 声明对象Singleton s3 = null ;/ 声明对象s1 = Singleton.getInstance() ;/ 取得实例化对象s2 = Singleton.getInstance() ;/ 取得实例化对象s3 = Singleton.getInstance() ;/ 取得实例化对象s1.print() ;/ 调用方法s2.print() ;/ 调用方法s3.print() ;/ 调用方法;/ 类名称 数组名称 = new 类名称长度Person per = new Person3 ;/ 开辟了三个空间大小的对象数组Person per = new Person(张三),new Person(李四),new Person(王五) ;class Outer/ 定义外部类private static String info = hello world ;/ 定义外部类的私有属性static class Inner/ 使用static定义内部类为外部类public void print()/ 定义内部类的方法System.out.println(info) ;/ 直接访问外部类的私有属性;public void fun()/ 定义外部类的方法new Inner().print() ;/ 通过内部类的实例化对象调用方法;public class InnerClassDemo03public static void main(String args)new Outer.Inner().print() ;/ 调用外部类的fun()方法;class Outer/ 定义外部类private String info = hello world ;/ 定义外部类的私有属性class Inner/ 定义内部类public void print()/ 定义内部类的方法System.out.println(info) ;/ 直接访问外部类的私有属性;public void fun()/ 定义外部类的方法new Inner().print() ;/ 通过内部类的实例化对象调用方法;public class InnerClassDemo04public static void main(String args)Outer out = new Outer();/ 外部类实例化对象Outer.Inner in = out.new Inner() ;/ 实例化内部类对象in.print() ;/ 调用内部类的方法;class Outer/ 定义外部类private String info = hello world ;/ 定义外部类的私有属性public void fun(final int temp)/ 定义外部类的方法class Inner/ 在方法中定义的内部类public void print()/ 定义内部类的方法System.out.println(类中的属性: + info) ;/ 直接访问外部类的私有属性System.out.println(方法中的参数: + temp );new Inner().print() ;/ 通过内部类的实例化对象调用方法;public class textpublic static void main(String args)new Outer().fun(30) ;/ 调用外部类的方法;class A;class B;class C extends A,B/ 错误,同时继承了两个父类;interface A/ 定义接口Apublic static final String AUTHOR = 李兴华 ;/ 全局常量public abstract void print() ;/ 抽象方法public abstract String getInfo() ;/ 抽象方法interface B/ 定义接口Bpublic void say() ;/ 定义抽象方法class X extends B implements A/ X类线继承B类,再实现A接口class A/ 定义类Apublic void fun1()/ 定义fun1()方法System.out.println(A - public void fun1() ;public void fun2()this.fun1() ;/ 调用fun1()方法;class B extends Apublic void fun1()/ 此方法被子类覆写了System.out.println(B - public void fun1() ;public void fun3()System.out.println(B - public void fun3() ;public class PolDemo01public static void main(String asrgs)B b = new B() ;/ 实例化子类对象A a = b ;/ 向上转型关系a.fun1() ;/ 此方法被子类覆写过a.fun3() ;/不能用,操作父类对象,无法找到子类方法;A a = new B() ;/ 向上转型关系B b = (B)a ;/ 发生了向下转型关系A a1 = new B() ;/ 通过向上转型实例化对象System.out.println(A a1 = new B(): + (a1 instanceof A) ;/trueSystem.out.println(A a1 = new B(): + (a1 instanceof B) ;/trueA a2 = new A() ;System.out.println(A a2 = new B(): + (a2 instanceof A) ;/trueSystem.out.println(A a2 = new B(): + (a2 instanceof B) ;/falseabstract class A/ 定义抽象类Apublic abstract void print() ;/ 定义抽象方法print();class B extends A / 定义子类,继承抽象类public void print()/ 覆写抽象方法System.out.println(Hello World!) ;public class AbstractCaseDemo01public static void main(String args)A a = new B() ;/ 通过子类为抽象类实例化a.print() ;Person per1 = null ;/ 声明Person对象Person per2 = null ;/ 声明Person对象per1 = new Student(张三,20,99.0f) ;/ 学生是一个人per2 = new Worker(李四,30,3000.0f) ;/ 工人是一个人per1.say() ;/ 学生说学生的话per2.say() ;/ 工人说工人的话结果:学生信息 - 姓名:张三;年龄:20;成绩:99.0工人信息 - 姓名:李四;年龄:30;工资:3000.0abstract class A/ 定义抽象类public abstract void printA() ;/ 抽象方法interface B/ 定义内部接口public void printB() ;/ 定义抽象方法;class X extends A/ 继承抽象类public void printA()System.out.println(HELLO - A) ;class Y implements B/ 定义内部类实现内部接口public void printB()System.out.println(HELLO - B) ;public class InnerExtDemo01public static void main(String args)A.B b = new X().new Y() ;b.printB() ;结果:HELLO - Binterface A/ 定义接口public void printA() ;/ 抽象方法abstract class B/ 定义内部抽象类public abstract void printB() ;/ 定义抽象方法;class X implements A/ 实现接口public void printA()System.out.println(HELLO - A) ;class Y extends B/ 继承抽象类public void printB()System.out.println(HELLO - B) ;public class InnerExtDemo02public static void main(String args)A.B b = new X().new Y() ;b.printB() ;结果:HELLO - Binterface USB/ 定义了USB接口public void start() ;/ USB设备开始工作public void stop() ;/ USB设备结束工作class Computerpublic static void plugin(USB usb)/ 电脑上可以插入USB设备usb.start() ;System.out.println(= USB 设备工作 =) ;usb.stop() ;class Flash implements USBpublic void start()/ 覆写方法System.out.println(U盘开始工作。) ;public void stop()/ 覆写方法System.out.println(U盘停止工作。) ;class Print implements USBpublic void start()/ 覆写方法System.out.println(打印机开始工作。) ;public void stop()/ 覆写方法System.out.println(打印机停止工作。) ;public class InterfaceCaseDemo02public static void main(String args)Computer.plugin(new Flash() ;Computer.plugin(new Print() ;interface Fruit/ 定义一个水果接口public void eat() ;/ 吃水果class Apple implements Fruitpublic void eat()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论