栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

无关紧要的事

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无关紧要的事

1.循环

public class q1 {
    public static void main(String[] args){
        int i,j;
        for (i=1;i<=9;i++){
            for (j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.print("n");
        }
    }
}


2.学校

class Student{
    String name;
    int age;
    static String school = "A大学";
    public Student(String name , int age){
        this.name = name;
        this.age = age;
    }
    public void info() {
        System.out.println("姓名:"+this.name+",年龄:"+this.age+",学校:"+this.school);
    }
}
public class Example14 {
    public static void main(String[] args){
        Student stu1=new Student("张三",18);
        Student stu2=new Student("李四",19);
        Student stu3=new Student("王五",20);
        stu1.info();
        stu2.info();
        stu3.info();
        stu1.school = "B大学";
        System.out.println("修改stu1学校对象的学校信息为B大学后");
        stu1.info();
        stu2.info();
        stu3.info();
    }
}


3.牧羊犬颜色

interface Animal {
    public String NAME = "牧羊犬";
    public void info();
}
interface Color {
    public void black();
}
interface Action extends Animal,Color{
    public void shout();
}
class  Dog implements Action{
    public void info() {
        System.out.println("名称:"+NAME);
    }
    public void black() {
        System.out.println("黑色");
    }
    public void shout() {
        System.out.println("汪汪......");
    }
}
class Example13 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.info();
        dog.shout();
        dog.black();
    }
}


4.猫狗多态

abstract class Animal {
    abstract void shout();
}
class Cat extends Animal {
    public void shout(){
        System.out.println("喵喵.......");
    }
}
class Dog extends Animal {
    public void shout() {
        System.out.println("汪汪......");
    }
}
public class Example14 {
    public static void main(String [] args) {
        Animal an1 = new Cat();
        Animal an2 = new Dog();
        an1.shout();
        an2.shout();
    }
}


5.小花喵喵

interface Animal{
    void shout();
}
public class 小花 {
   public static void main (String [] args) {
       String name = "小花";
       animalShout(new Animal() {
           @Override
                   public void shout() {
               System.out.println(name+"喵喵");
           }
       });
   }
   public static void animalShout(Animal an){
       an.shout();
   }
}



6.io1

import java.io.File;

public class Example1 {
    public static void main(String [] args){
        File f = new File("D:\file\a.txt");
        File f1 = new File("src\Hello.java");
        System.out.println(f);
        System.out.println(f1);
    }
}




7.io2

import java.io.File;
import java.io.IOException;

public class Example3  {
    public static void main(String[]args)throws IOException {
        File f=File.createTempFile("incast-",".txt");
        f.deleteOnExit();
        System.out.println("f是否为文件:"+f.isFile());
        System.out.println("f的相对路径:"+f.getPath());
    }
}



8.字节流读

import  java.io.*;
public class ZH1 {
    public static void main(String[] args)throws Exception{
        FileInputStream in=new FileInputStream("test.txt");
        int b=0;
        while(true){
            b=in.read();
            if(b==-1){
                break;
            }
            System.out.println(b);
        }
        in.close();
    }
}




9.字节流写

import java.io.*;
public class ZH3 {
    public static void main(String[] args)throws Exception{
        OutputStream out=new FileOutputStream("example2.txt",true);
        String str="欢迎你";
        byte[] b=str.getBytes();
        for(int i=0;i 0) {
            Thread th = Thread.currentThread();
            String th_name = th.getName();
            System.out.println(th_name + "正在发售第" + tickets-- + "张票");
        }
    }
}



14.卖票

public class SH6 {
    public static void main(String[] args){
        TicketWindow tw=new TicketWindow();
        new Thread(tw,"窗口1").start();
        new Thread(tw,"窗口2").start();
        new Thread(tw,"窗口3").start();
        new Thread(tw,"窗口4").start();
    }
}
class TicketWindow extends Thread {
    private int tickets = 100;

    public void run() {
        while (tickets > 0) {
            Thread th = Thread.currentThread();
            String th_name = th.getName();
            System.out.println(th_name + "正在发售第" + tickets-- + "张票");
        }
    }
}



15.文件复制2

import java.io.*;
public class ZH5 {
    public static void main(String[] args)throws Exception{
        InputStream in=new FileInputStream("source/a.png");
        OutputStream out=new FileOutputStream("target/a.png");
        byte[] buff=new byte[1024];
        int len;
        long begintime=System.currentTimeMillis();
        while ((len= in.read(buff))!=-1){
            out.write(buff,0,len);
        }
        long endtime=System.currentTimeMillis();
        System.out.println("复制文件所消耗的时间是:"+(endtime-begintime)+"ms");
        in.close();
        out.close();
    }
}




16.接io1后 try finall

public static void main(String[] args)throws Exception{

InputStream input = null;

try{

FileInputStream in = new FileInputStream("test.txt");

int b= 0;

while(true){

b = in.read();

if(b==-1){

break;

}

System.out.println(b);

}

}finally{

if(input!=null){

input.close();

}

}

}

1.循环

2.学校

class Student{
    String name;
    int age;
    static String school = "A大学";

    public Student(String name , int age){
        this.name = name;
        this.age = age;
    }
    public void info() {
        System.out.println("姓名:"+this.name+",年龄:"+this.age+",学校:"+this.school);
    }
}
public class Example14 {
    public static void main(String[] args){
        Student stu1=new Student("张三",18);
        Student stu2=new Student("李四",19);
        Student stu3=new Student("王五",20);
        stu1.info();
        stu2.info();
        stu3.info();
        stu1.school = "B大学";
        System.out.println("修改stu1学校对象的学校信息为B大学后");
        stu1.info();
        stu2.info();
        stu3.info();
    }
}

3.牧羊犬颜色

interface Animal {
    public String NAME = "牧羊犬";
    public void info();
}
interface Color {
    public void black();
}
interface Action extends Animal,Color{
    public void shout();
}
class  Dog implements Action{
    public void info() {
        System.out.println("名称:"+NAME);
    }
    public void black() {
        System.out.println("黑色");
    }
    public void shout() {
        System.out.println("汪汪......");
    }
}
class Example13 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.info();
        dog.shout();
        dog.black();
    }
}
 

4.猫狗多态

abstract class Animal {
    abstract void shout();
}
class Cat extends Animal {
    public void shout(){
        System.out.println("喵喵.......");
    }
}
class Dog extends Animal {
    public void shout() {
        System.out.println("汪汪......");
    }
}
public class Example14 {
    public static void main(String [] args) {
        Animal an1 = new Cat();
        Animal an2 = new Dog();
        an1.shout();
        an2.shout();
    }
}
 

5.小花喵喵

interface Animal{
    void shout();
}
public class 小花 {
   public static void main (String [] args) {
       String name = "小花";
       animalShout(new Animal() {
           @Override
                   public void shout() {
               System.out.println(name+"喵喵");
           }
       });
   }
   public static void animalShout(Animal an){
       an.shout();
   }
}

6.io1

import java.io.File;

public class Example1 {
    public static void main(String [] args){
        File f = new File("D:\file\a.txt");
        File f1 = new File("src\Hello.java");
        System.out.println(f);
        System.out.println(f1);
    }
}
 

7.io2

import java.io.File;
import java.io.IOException;

public class Example3  {
    public static void main(String[]args)throws IOException {
        File f=File.createTempFile("incast-",".txt");
        f.deleteOnExit();
        System.out.println("f是否为文件:"+f.isFile());
        System.out.println("f的相对路径:"+f.getPath());
    }
}

8.字节流读

9.字节流写

10.文件复制

11.线程

12.多线程

13.线程与多线程对比

14.卖票

public class SH6 {
    public static void main(String[] args){
        TicketWindow tw=new TicketWindow();
        new Thread(tw,"窗口1").start();
        new Thread(tw,"窗口2").start();
        new Thread(tw,"窗口3").start();
        new Thread(tw,"窗口4").start();
    }
}
class TicketWindow extends Thread {
    private int tickets = 100;

    public void run() {
        while (tickets > 0) {
            Thread th = Thread.currentThread();
            String th_name = th.getName();
            System.out.println(th_name + "正在发售第" + tickets-- + "张票");
        }
    }
}

15.文件复制2

16.接io1后 try finall

public static void main(String[] args)throws Exception{

InputStream input = null;

try{

FileInputStream in = new FileInputStream("test.txt");

int b= 0;

while(true){

b = in.read();

if(b==-1){

break;

}

System.out.println(b);

}

}finally{

if(input!=null){

input.close();

}

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号