** Choose the correct description for the interface. (2) **
A. Even if the access modifier is omitted, it is treated as a public method. B. No fields can be defined. C. Class cannot realize multiple interfaces at the same time. D. Interfaces cannot be inherited. E. Abstract classes do not have to implement the methods defined in the interface
The answer is ** AE **
A. Even if the access modifier is omitted, it is treated as a public method. ... Correct, you can't use protected or private for the interface. B. No fields can be defined. ... Incorrect answer, made possible by using final and static in the specifications from JAVASE8. C. Class cannot realize multiple interfaces at the same time. ... Incorrect, the java interface is there to solve inheritance problems such as c ++ diamond inheritance. Instead of the class prohibiting multiple inheritance, the interface allows it. D. Interfaces cannot be inherited. ・ ・ ・ Incorrect answer, it is possible to inherit the interface. E. Abstract classes do not have to implement the methods defined in the interface ... Correct answer, if you inherit them and enter all the instances that are finally realized, there is no problem.
Let's think about this as TypeScript.
** A. Even if the access modifier is omitted, it is treated as a public method. ** ** In js, if the access modifier is omitted, it will be treated as public in java. Since typescript is compatible with js, omitting the access modifier makes it accessible from anywhere as well.
** B. No fields can be defined. ** **
typescript.ts
interface MyInterface{
name:string
}
class MyClass implements MyInterface{
name:string;//If this member does not exist, a compile error will occur.
constructor(name: string) {
this.name = name;
}
}
var obj:MyClass = new MyClass("masao");
var str:string = obj.name;
console.log(str);//Display as masao
After compilation
typescript.ts
var MyClass = (function () {
function MyClass(name) {
this.name = name;
}
return MyClass;
}());
var obj = new MyClass("masao");
var str = obj.name;
console.log(str); //Display as masao
It is possible to compile normally even if you specify the field.
** C. Class cannot realize multiple interfaces at the same time. ** **
TypeScript.ts
interface MyInterface{
name:string;
}
interface MyInterface2{
family:string;
}
class MyClass implements MyInterface,MyInterface2{
name:string;//If this member does not exist, a compile error will occur.
family:string;//If this member does not exist, a compile error will occur.
constructor(name: string,family: string) {
this.name = name;
this.family = family;
}
}
var obj:MyClass = new MyClass("masao","yamda");
var str:string = obj.name + obj.family;
console.log(str);//Displayed as masao yamada
After compilation
javascript.js
var MyClass = (function () {
function MyClass(name, family) {
this.name = name;
this.family = family;
}
return MyClass;
}());
var obj = new MyClass("masao", "yamda");
var str = obj.name + obj.family;
console.log(str); //Displayed as masao yamada
Even if you realize multiple interfaces, you can compile normally.
** D. Interface cannot be inherited. ** **
typeScript
interface MyInterface{
name:string;
}
interface MyInterface2 extends MyInterface{
family:string;
}
class MyClass implements MyInterface2{
name:string;//If this member does not exist, a compile error will occur.
family:string;//If this member does not exist, a compile error will occur.
constructor(name: string,family: string) {
this.name = name;
this.family = family;
}
}
var obj:MyClass = new MyClass("masao","yamda");
var str:string = obj.name + obj.family;
console.log(str);//Displayed as masao yamada
After compilation
JavaScript.js
var MyClass = (function () {
function MyClass(name, family) {
this.name = name;
this.family = family;
}
return MyClass;
}());
var obj = new MyClass("masao", "yamda");
var str = obj.name + obj.family;
console.log(str); //Displayed as masao yamada
Even if the interface inherits, it will compile without any problem.
** E. The abstract class does not have to implement the methods defined in the interface ** TypeScript has no abstract classes.
Recommended Posts