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<b.length;i++){
out.write(b[i]);
}
out.close();
}
}
10.文件复制
import java.io.*;
public class ZH4 {
public static void main(String[] args)throws Exception{
InputStream in=new FileInputStream("source/a.png");
OutputStream out=new FileOutputStream("target/b.png");
int len;
long begintime=System.currentTimeMillis();
while ((len=in.read())!=-1){
out.write(len);
}
long endtime=System.currentTimeMillis();
System.out.println("复制文件所消耗的时间是:"+(endtime-begintime)+"ms");
in.close();
out.close();
}
}
11.线程
public class SH2 {
public static void main(String[] args) {
MyThread02 myThread02 = new MyThread02();
myThread02.start();
while (true){
System.out.println("main方法在运行");
}
}
}
class MyThread02 extends Thread{
public void run(){
while (true){
System.out.println("MyThread类的run()方法在运行");
}
}
}
12.多线程
public class SH3 {
public static void main(String[] args) {
MyThread03 myThread = new MyThread03();
Thread thread=new Thread(myThread);
thread.start();
while (true){
System.out.println("main()方法在运行");
}
}
}
class MyThread03 implements Runnable{
public void run(){
while (true){
System.out.println("MyThread类的run方法在运行0");
}
}
}
13.线程与多线程对比
public class SH5 {
public static void main(String[] args){
new TicketWindow().start();
new TicketWindow().start();
new TicketWindow().start();
new TicketWindow().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-- + "张票");
}
}
}
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.循环
/*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<b.length;i++){
out.write(b[i]);
}
out.close();
}
}
*/
10.文件复制
/*import java.io.*;
public class ZH4 {
public static void main(String[] args)throws Exception{
InputStream in=new FileInputStream("source/a.png");
OutputStream out=new FileOutputStream("target/b.png");
int len;
long begintime=System.currentTimeMillis();
while ((len=in.read())!=-1){
out.write(len);
}
long endtime=System.currentTimeMillis();
System.out.println("复制文件所消耗的时间是:"+(endtime-begintime)+"ms");
in.close();
out.close();
}
}
*/
11.线程
/*public class SH2 {
public static void main(String[] args) {
MyThread02 myThread02 = new MyThread02();
myThread02.start();
while (true){
System.out.println("main方法在运行");
}
}
}
class MyThread02 extends Thread{
public void run(){
while (true){
System.out.println("MyThread类的run()方法在运行");
}
}
}
*/
12.多线程
/*public class SH3 {
public static void main(String[] args) {
MyThread03 myThread = new MyThread03();
Thread thread=new Thread(myThread);
thread.start();
while (true){
System.out.println("main()方法在运行");
}
}
}
class MyThread03 implements Runnable{
public void run(){
while (true){
System.out.println("MyThread类的run方法在运行0");
}
}
}*/
13.线程与多线程对比
/*public class SH5 {
public static void main(String[] args){
new TicketWindow().start();
new TicketWindow().start();
new TicketWindow().start();
new TicketWindow().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-- + "张票");
}
}
}*/
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();
}
}
}