上面是期末考试老师给的题目中六道题目,其他的好不容易搞出来了,但这些不会啊
我本人喜欢linux和php,java是学校的规定课程,对这个既不擅长,又不感兴趣
上面的题目可能是考试的题目
求大神高抬贵手,指点一下吧
27就考试了,最好还是今天搞好
我本人喜欢linux和php,java是学校的规定课程,对这个既不擅长,又不感兴趣
上面的题目可能是考试的题目
求大神高抬贵手,指点一下吧
27就考试了,最好还是今天搞好
public void setName(String name) {
this.name.add(name);
}
public void setNum(int num) {
this.num = num;
}
private int num;
ArrayList<String> name = new ArrayList<String>(); public void displayname(){
System.out.print("The student name: ");
for(int i=0; i<name.size(); i++)
System.out.print(name.get(i) + " ");
System.out.println();
}
public void displaynum(){
System.out.println("The number of student: " + num);
}
}
验证:package csdn.programbbs_625;public class TestStudent {
public static void main(String[] args) {
Student stu = new Student();
stu.setNum(5);
stu.setName("aaa");
stu.setName("bbb");
stu.setName("abc");
stu.setName("cba");
stu.displayname();
stu.displaynum();
}}
package csdn.programbbs_625;public class Person {
public String toString() {
return name + " " + sex + " " + id + " " + phone + " " + email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
public Person(String name, char sex, String id) {
super();
this.name = name;
this.sex = sex;
this.id = id;
}
private String name;
private String id;
private char sex;
private String phone;
private String email;
}
TextPerson类:
package csdn.programbbs_625;public class TextPerson {
public static void main(String[] args) {
Person aPerson = new Person("张三", '男', "220203197801010001");
aPerson.setEmail("[email protected]");
aPerson.setPhone("13074318888");
System.out.println(aPerson.toString());
}}
package csdn.programbbs_625;import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;public class TextJButton extends JFrame{
public TextJButton(){
this.setSize(350, 370);
this.setVisible(true);
this.setLayout(new FlowLayout());
final JButton jb = new JButton("按钮");
jb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
jb.setText("点击按钮");
repaint();
}
});
this.add(jb);
} public static void main(String[] args) {
new TextJButton();
}
}
package zhangming.csdn.javase;class MyThread implements Runnable
{
private int ticket=100;
public void run()
{ while(ticket > 0)
{
//先睡眠,后锁定,以便让其他线程进来
try
{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
//在进行售票时,需要进行锁定
synchronized(this)
{
if(ticket>0)
{
System.out.println(Thread.currentThread().getName()+"卖掉第"+ticket--+"张票");
}
}
}
}
}
public class SynchronizedDemo
{
public static void main(String args[])
{
MyThread mt = new MyThread();
Thread t1 = new Thread(mt,"窗口1");
Thread t2 = new Thread(mt,"窗口2");
Thread t3 = new Thread(mt,"窗口3");
Thread t4 = new Thread(mt,"窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
第四题package csdn.programbbs_625;
import java.lang.Thread;class MyThread1 extends Thread{
public void run() {
System.out.println("aaaaaa");
}
}
class MyThread2 extends Thread{
public void run() {
System.out.println("bbbbbbbb");
}
}
class MyThread3 implements Runnable {
public int x = 0;
public void run() {
System.out.println(++x);
}
}public class TextThread {
public static void main(String[] args) {
Thread t1 = new MyThread1();
Thread t2 = new MyThread2();
synchronized (t1) {
t1.start();
}
MyThread3 r = new MyThread3();
for (int i = 0; i < 10; i++) {
Thread t3 = new Thread(r);
t3.start();
}
t2.start();
}
}
package csdn.programbbs_625;class Threadmy implements Runnable
{
private int ticket=0;
public void run()
{ while(ticket < 100)
{
//先睡眠,后锁定,以便让其他线程进来
try
{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
//在进行售票时,需要进行锁定
synchronized(this)
{
if(ticket < 100)
{
System.out.println(Thread.currentThread().getName()+"卖掉第" + ++ticket +"张票");
}
}
}
}
}
public class SynchronizedDemo
{
public static void main(String args[])
{
Threadmy mt = new Threadmy();
Thread t1 = new Thread(mt,"窗口1");
Thread t2 = new Thread(mt,"窗口2");
Thread t3 = new Thread(mt,"窗口3");
Thread t4 = new Thread(mt,"窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package csdn.programbbs_625;
//死锁的实现
class A
{
public void get(){
System.out.println("A说:我开始启动了,B,给我你的资源");
}
public void say(){
System.out.println("A获得资源");
}
}
class B
{
public void get(){
System.out.println("B说:我开始启动了,A,给我你的资源");
}
public void say(){
System.out.println("B获得资源");
}
}
class MyThread implements Runnable
{
public static A a = new A();
public static B b = new B();
public boolean flag = false;
public void run(){
if(flag){
synchronized(a){
a.get();
try{
Thread.sleep(500);
}catch(InterruptedException e){}
synchronized(b){ //此同步代码块在另一同步代码块里
a.say();
}
}
}else{
synchronized(b){
b.get();
try{
Thread.sleep(500);
}catch(InterruptedException e){}
synchronized(a){ //此同步代码块在另一同步代码块里
b.say();
}
}
}
}
}
public class Demo24
{
public static void main(String args[]){
MyThread mt1 = new MyThread();
MyThread mt2 = new MyThread();
mt1.flag=true;
mt2.flag=false;
Thread th1 = new Thread(mt1);
Thread th2 = new Thread(mt2);
th1.start();
th2.start();
}
}