hello world 的各种输出方式(1)闲的蛋疼

本文介绍了一个简单的Java程序,通过面向对象的设计实现不同类之间的交互。包括成员变量、静态工厂方法、接口实现、抽象类及继承等核心概念。展示了如何创建类、使用静态实例和工厂模式获取对象实例、实现接口方法、定义抽象类以及使用匿名内部类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Person{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
class Member{
    private Member(){

    }
    private static  Member instance=new Member();
    public static Member getInstance(){
        return instance;
    }
    public void print(){
        System.out.println("hello word");
    }
}
class Student{
    private String num;
    private Student(String num){
        this.num=num;
    }
    private static final int mem=10;
    private static final Student M1=new Student("hello word");
    public static Student getM(int m){
        switch(m){
        case mem:
            return M1;
            default:
                return null;
        }
    }
    public String toString(){
        return this.num;
    }
}
interface Iprint{
    public void print();
}
class Final implements Iprint{
    public void print(){
        System.out.println("word");
    }
}
class Poly implements Iprint{
    private Final f;
    public Poly(Final f){
        this.f=f;
    }
    public void first(){
        System.out.println("hello");
    }
    public void print(){
        first();
        this.f.print();
    }
}
class Factory{
    public static Iprint getInstance(){
        return new Poly(new Final());
    }
}
interface In{
    public void print();
}
abstract class Father{
    public abstract void println();
}
class Son extends Father{

    public void println() {
        // TODO Auto-generated method stub
        System.out.println("hello word");
    }

}
class Array{
    private String h="helloword";
    char[] data=h.toCharArray();
    public void print(){
        for(int x=0;x<data.length;x++){
            System.out.print(data[x]+" ");
        }
    }
}
class Link{
    private class Node{
        private Node next;
        private Object data;
        public Node(Object data){
            this.data=data;
        }
        public void addNode(Node node){
            if(this.next==null){
                this.next =node;
            }else{
                this.next.addNode(node);
            }
        }
        public void toArrayNode() {
            Link.this.reData[Link.this.foot++]=this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }
        }
    }
    private int count;
    private Node root;
    private int foot;
    private Object[]reData;
    public void add(Object data){
        if(data==null){
            return;
        }
        Node node =new Node(data);
        if(this.root==null){
            this.root=node;
        }else{
            this.root.addNode(node);
        }count++;
    }
    public Object[]toArray(){
        if(this.count==0){
            return null;
        }
        this.reData=new Object[this.count];
        this.foot=0;
        this.root.toArrayNode();
        return this.reData;
    }
}
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello word");
        Member mem=Member.getInstance();
        mem.print();
        System.out.println(Student.getM(10));
        Iprint i=Factory.getInstance();
        i.print();
        new In(){
            public void print(){
                System.out.println("hello word");
            }
        }.print();
        Father f=new Son();
        f.println();
        new Array().print();
        Person p=new Person();
        p.setName("hello word");
        System.out.println();
        System.out.println(p.getName());
        Link l=new Link();
        l.add("hello");
        l.add("word");
        Object[]data=l.toArray();
        for(int x=0;x<data.length;x++){
            System.out.print(data[x]+" ");
        }
        }

}

Hello word
hello word
hello word
hello
word
hello word
hello word
h e l l o w o r d
hello word
hello word

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值