Java – the question I tripped over in the interview

I was asked several Java interview questions. I don't know how to solve them. I can use some answers

>Write a method that accepts int so that the method is OK

public void somemethd(int i){}

Now, if I = 1, it should print true, if I = 2, it should print false, but you can't use any conditional operators, nor can you use if or switch > how two threads pass data between each other. For example, let's say you start two threads, and you want one thread to make the other wait He rewrites the problem to make it simpler. If I have a variable in one thread, what do I want to do if I want to send it to another thread. > If there is a non static method in the base class and you make it a static method in the derived class, you will get the same error. Just as you have a static method in the base, you try to make it a non static method in the derived class > what is the use of overloading other than the same name for different methods? (I think I should say constructor overloading, but it just didn't come to me, what else) > the syntax is incorrect, but you can assume that everything is fine

class base{

show(){

this.mymethod(); }

mymethod(){ /... } }

class derived extends base{

show(){

super.mymethod();
}

mymethod(){
/...
}
}

现在当我做以下事情时会发生什么

一个.

base b = new derived();

   b.show(); 

derived d= new base();

   d.show();

C.如果基地没有显示方法我做了什么呢?

 base b = new derived();

   b.show(); 

d.如果基地没有显示方法我做了什么呢?

 derived d = new base();

   d.show();

show(){

super.mymethod();
}

mymethod(){
/...
}
}base b = new derived();

b.show();derived d= new base();

d.show();base b = new derived();

b.show();

Solution

Updated one solution 1)

void printIsOne(int n) {
   System.out.println("?,true,false".split(",")[n]);
}

2) You cannot pass local variables between threads However, you can pass a value from one thread to another and let it wait for the value With all due respect: the easiest way is to use BlockingQueue

3) Different errors occur in both cases, but basically you can't change one of them to the other in a derived class

4) Backward comparability You may have code, such as a lot of test code, which uses a set of args to call methods, but you want to change it in some way without changing the existing code

5) Try it (hint: some combinations get compiler errors)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>