java一些代码_一些java小代码

创建一个简单的窗体

1)

import java.awt.*;

import javax.swing.*;

class hello extends JFrame {

hello(){

setTitle("hello");

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new hello();

}

}

当然也可以使用另外一种不是很推荐的方法:

import java.awt.*;

import javax.swing.*;

class hello {

public static void main(String[] args) {

JFrame frame= new JFrame();

frame.setSize(100,100);

frame.setTitle("hello");

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

效果都如下所示:

class hello {

public static void main( String[] args ) {

String[] he=new String[3];

he[0]="1";

he[1]="2";

he[2]="3";

for(String i : he){

System.out.println(i);

}

}

}

输出结果:1 2 3

import javax.swing.*;

public class hello {

public static void main( String[] args ) {

JOptionPane.showInputDialog("Rollen Holt");

JOptionPane.showMessageDialog(null, "Welcome");

}

}

import java.text.NumberFormat;

public class hello {

public static void main( String[] args ) {

NumberFormat format1 = NumberFormat.getCurrencyInstance();

double a = 1000.1020;

System.out.println(format1.format(a));

}

}

输出结果:¥1,000.10

import java.math.*;

public class hello {

public static void main( String[] args ) {

long a=10000000000000l;

long b=20000000000000l;

BigInteger A=BigInteger.valueOf(a);

BigInteger B= BigInteger.valueOf(b);

// 对于大树类 不能直接用加减乘除,只能采用函数调用的形式

BigInteger str=A.multiply(B);

System.out.println(str);

}

}

输出的结果:200000000000000000000000000

// 对象的引用传递 1

class hello{

public static void main(String[] args){

String str="hello";

function(str);

System.out.println(str);

}

private static void function(String str2){

str2="world";

}

}

输出结果:hello (因为string类型是不可变的)

对象的引用传递2

// 对象的引用传递 2

class Demo {

String str = "hello"; //注意这里是string类型,类类型

}

public class hello {

public static void main( String[] args ) {

Demo demo = new Demo();

demo.str = "world";

System.out.println(demo.str);

function(demo);

System.out.println(demo.str);

}

static void function( Demo demo ) {

demo.str = "Rollen";

}

}

输出的结果是:

world

Rollen

// 对象的引用传递 3

class Demo {

int str = 1; // 这里是int类型,是基本类型

}

public class hello {

public static void main(String[] args) {

Demo demo = new Demo();

demo.str = 2;

System.out.println(demo.str);

function(demo);

System.out.println(demo.str);

}

static void function(Demo demo) {

demo.str = 3;

}

}

输出的结果:

2

3

/**

* @version 1.0

* @author Rollen-Holt 判断两个一般对象是否相等

* */

class hello{

hello(){

name = "";

age = 0;

}

hello(String name, int age){

this.age = age;

this.name = name;

}

public boolean compare(hello he){

if (he == null) {

return false;

}

if (he == this) { //如果两个对象在同一个地址的话,一定是相等的

return true;

} // 否则的话,就要逐个比较内容了

if (this.name.equals(he.name) && (this.age == he.age)) {

return true;

} else {

return false;

}

}

public static void main(String[] args){

hello h1 = new hello("name1", 12);

hello h2 = new hello("name2", 18);

if (h1.compare(h2)) {

System.out.println("equal");

} else {

System.out.println("not equal");

}

}

private String name;

private int age;

}

输出的结果:not equal

有时候我们还需要在继承的情况下判断对象是否相等

/**

* @author Rollen-Holt

* 继承中的对象的比较

*/

class Person{

Person(String name, int age){

this.age=age;

this.name=name;

}

public boolean equals(Object obj){

if(obj==null){

return false;

}

if(this==obj){

return true;

}

if(!(obj instanceof Person)){

return false;

}

Person per =(Person)obj;

if(this.name.equals(per.name)&&(this.age==per.age)){

return true;

}else{

return false;

}

}

public String toString(){

return this.name+""+this.age;

}

private String name;

private int age;

}

class hello{

public static void main(String[] a){

Person per1=new Person("Rollen",12);

Person per2=new Person("Rollen",12);

if(per1.equals(per2)){

System.out.println("equals");

}else{

System.out.println("Not equals");

}

}

}

/**

* @version 1.0

* @author Rollen-Holt 代码块的使用

* */

class hello{

static{

System.out.println("hello");

System.exit(1);

}

}

/**

* @version 1.0

* @author Rollen-Holt 代码块的使用,注意调用顺序

* */

class hello{

{

// 普通代码块

System.out.println("普通代码块");

}

static{

//静态代码块

System.out.println("静态代码块");

}

hello(){

//构造代码块

System.out.println("构造代码块");

}

public static void main(String[] a0){

hello he=new hello();

hello he1=new hello();

System.out.println("主函数");

}

}

输出结果:

静态代码块 // 静态代码块指调用一次

普通代码块

构造代码块

普通代码块

构造代码块

主函数

/**

* @version 1.0

* @author Rollen-Holt 使用static 定义内部类 ,但是静态内部类只能访问外部类的静态属性和方法

* */

class hello{

private static void say(){

System.out.print("hello");

}

private void say1(){

System.out.print("hello1");

}

static class inner{

void getSay(){

say();

}

//void getSay1(){

//say1();

//}

}

public static void main(String[] a0){

new hello.inner().getSay();

//new hello.inner().getSay1(); // 静态内部类不能发访问外部类的非静态成员和方法

}

}

/**

* @version 1.0

* @author Rollen-Holt

* 继承中的方法重写的问题

* */

class Demo{

void say(){

System.out.println("Demo");

}

}

class hello extends Demo{

public void say(){ //注意这里的public,重写的时候访问权限不能小于原来的

System.out.println("hello");

}

public static void main(String[] a0){

new hello().say();

}

}

/**

* @author Rollen-Holt 在抽象类中定义构造方法

*/

abstract class Demo{

public Demo(){

System.out.println("Demo");

}

public void say(){

System.out.println("Rollen");

}

}

class hello extends Demo{

public void say(){

super.say();

System.out.println("Holt");

}

public static void main(String[] a){

new hello().say();

}

}

输出的结果:

Demo

Rollen

Holt

/**

* @author Rollen-Holt this 关键字的使用

*/

class hello{

hello(){

name = "";

age = 0;

}

// 此处使用this访问类属性

hello(String name, int age){

this.age = age;

this.name = name;

}

// 此处使用this调用构造函数

hello(String name){

this(name, 0);

}

public String getName(){

return name;

}

public static void main(String[] a){

hello he = new hello("Holt");

System.out.println(he.getName());

}

private int age;

private String name;

}

关于接口的一点点啰嗦:

//接口中的方法必须是public。如果不写public的话,默认也是public的

interface A{

public static final String name="Rollen";

public abstract void say();

}

接口其实可、可以简写为下面的形式:

interface A{

String name = "Rollen";

void say();

}

/**

* @author Rollen-Holt

* 接口的一个例子

*/

// 接口中的方法必须是public。如果不写public的话,默认也是public的

interface A{

String name = "Rollen";

void say();

}

class hello implements A{

public static void main(String[] a){

new hello().say();

}

@Override

public void say(){

System.out.println(name);

}

}

/**

* @author Rollen-Holt

* 对象的多态性

* 对象的向上传递

*/

class Demo{

public void say(){

System.out.println("Rollen");

}

}

class hello extends Demo{

public void say(){

System.out.println("Holt");

}

public static void main(String[] a){

hello he=new hello();

Demo demo=new Demo();

demo=he;

demo.say();

}

}

输出结果Holt

/**

* @author Rollen-Holt

* 对象的多态性

* 对象的向下传递

*/

class Demo{

public void say(){

System.out.println("Rollen");

}

}

class hello extends Demo{

public void say(){

System.out.println("Holt");

}

public static void main(String[] a){

Demo demo=new hello();

//但是如果将上面的这句换为: Demo demo=new Demo();

//将会出现错误

hello he=(hello)demo;

he.say();

}

}

输出结果:Holt

/**

* @author Rollen-Holt

*/

// 在抽象类内部也可以定义多个抽象类或者是接口,

//在接口的内部也可以定义多个接口或者是抽象类

abstract class A{

public abstract void sayA();

interface B{

public void sayB();

}

}

class X extends A{

public void sayA(){

System.out.println("A");

}

class Y implements B{

public void sayB(){

System.out.println("B");

}

}

}

class hello{

public static void main(String[] a){

A.B demo=new X().new Y();

demo.sayB();

}

}

/**

* @author Rollen-Holt

*/

// 在抽象类内部也可以定义多个抽象类或者是接口,

// 在接口的内部也可以定义多个接口或者是抽象类

/*

* 在一个接口中实现抽象类

*/

interface A{

public void sayA();

abstract class B{

public abstract void sayB();

}

}

class X implements A{

public void sayA(){

System.out.println("A");

}

class Y extends B{

public void sayB(){

System.out.println("B");

}

}

}

class hello{

public static void main(String[] a){

A.B demo =new X().new Y();

demo.sayB();

}

}

Object类提供的equals()方法默认是比较地址的。

/**

* @author Rollen-Holt

* 使用object 接收接口

*/

interface A{

public void say();

}

class B implements A{

public void say(){

System.out.println("A");

}

}

class hello{

public static void main(String[] args){

A a=new B();

Object obj=a;

A demo=(A)obj;

demo.say(); //输出A

}

}

/*

* 这个实例说明,虽然接口不能继承类,但是接口仍然是Object的子类。

* 因为接口本身就是引用数据类型 所以可以进行向上的转型操作

*

*/

/**

* @author Rollen-Holt 使用object 接收数组

*/

class hello{

public static void print(Object obj){

if (obj instanceof int[]) {

int[] array = (int[]) obj;

for(int temp : array) {

System.out.println(temp + "\t");

}

}

}

public static void main(String[] args){

int temp[] = { 1, 2, 3, 4, 5 };

Object obj = temp;

print(obj);

}

}

/*

* Object 可以接受任何引用类型

*/

关于异常的一点点小解说:

Exception在程序中必须使用try …catch语句进行处理

但是RuntimeException可以不使用try…catch进行处理,如果期间有异常产生,则直接提交到JVM

一点点小建议:

对于RuntimeException也使用try…catch语句进行处理

关于使用分别倒入和使用“*”导入那个效率高的问题:

其实效率是一样的,因为即使对于“*”导入的,也是程序自动加载的。而不需要的类不会被导入到程序。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值