package forth; // class liu{} interface Drawable{ void draw(); } class Doctor implements Drawable{ public void draw() { System.out.println("Painting Cartoon"); } } class Painter implements Drawable{ //instance variables - replace the example below with your own public void draw() { System.out.println("Draw Mountain Scence"); } } public class five{ public static void main(String[] args) { Doctor _d = new Doctor(); Painter _p = new Painter(); DPdraw(_d); DPdraw(_p); } public static void DPdraw(Drawable _dI) { _dI.draw(); } }简单解释一下
时间: 2024-02-23 21:57:24 浏览: 28
这段代码定义了一个接口Drawable,该接口中只有一个抽象方法draw()。然后定义了两个类Doctor和Painter,它们都实现了Drawable接口,因此都必须实现draw()方法。在main函数中,分别创建了一个Doctor对象和一个Painter对象,并分别传入DPdraw()方法中进行调用。DPdraw()方法的参数是一个Drawable类型的接口,因此可以接受实现了Drawable接口的任何类的对象,并调用其draw()方法。最终输出结果是调用Doctor对象的draw()方法,输出"Painting Cartoon",调用Painter对象的draw()方法,输出"Draw Mountain Scence"。
相关问题
public abstract class Shape { public abstract double getArea();}public interface Drawable { void draw();}public class Rectangle extends Shape implements Drawable { private double width; private double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } @Override public double getArea() { return width * height; } @Override public void draw() { System.out.println("Drawing a rectangle."); }}
这段代码展示了一个抽象类 Shape 和一个接口 Drawable,以及一个实现了 Shape 和 Drawable 的具体类 Rectangle。Shape 类是一个抽象类,其中定义了一个抽象方法 getArea(),这个方法没有实现,需要在子类中具体实现。Drawable 接口中定义了一个方法 draw(),任何实现了 Drawable 接口的类都必须实现 draw() 方法。Rectangle 类继承自 Shape 并实现了 Drawable 接口,它有一个构造方法,可以创建一个指定宽度和高度的矩形。在 Rectangle 类中,实现了 Shape 中的抽象方法 getArea() 和 Drawable 中的方法 draw(),getArea() 方法计算矩形的面积,draw() 方法打印出 "Drawing a rectangle."。这个例子展示了面向对象的多态特性,通过抽象类和接口,我们可以定义出一些通用的方法和属性,并让具体的类来实现这些方法和属性,从而实现代码的灵活性和复用性。
@drawable/key_background 代码
`@drawable/key_background` 是一个自定义的 Drawable 对象,用于设置 Keyboard 的 Key 背景样式。您可以根据自己的需要创建一个类似的 Drawable 对象,下面是一个简单的例子:
```
<!-- key_background.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 普通状态下的 Key 背景 -->
<item android:drawable="@drawable/normal_key_bg" />
<!-- 按下状态下的 Key 背景 -->
<item android:drawable="@drawable/pressed_key_bg" android:state_pressed="true" />
<!-- 选中状态下的 Key 背景 -->
<item android:drawable="@drawable/selected_key_bg" android:state_selected="true" />
</selector>
```
在上面的代码中,我们使用了一个 `<selector>` 标签来定义不同状态下的 Key 背景。这里有三个状态:普通状态、按下状态和选中状态。对于每个状态,我们可以分别设置对应的背景图片,例如 `@drawable/normal_key_bg` 表示普通状态下的背景图片。您可以根据需要自定义不同状态下的背景图片。
接下来,我们来看看如何实现一个简单的圆角矩形背景图片。这里我们使用 `<shape>` 标签来定义一个矩形形状,并设置圆角半径和背景颜色:
```
<!-- normal_key_bg.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="@color/key_normal_bg_color" />
</shape>
```
在上面的代码中,我们使用了 `<corners>` 标签来设置圆角半径,使用 `<solid>` 标签来设置背景颜色。您可以根据需要调整这些值来实现不同的背景样式。
最后,我们来看看如何实现一个带有按下效果的背景图片。这里我们使用 `<selector>` 标签来定义不同状态下的背景图片,并设置对应状态下的透明度:
```
<!-- pressed_key_bg.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态下的背景 -->
<item android:drawable="@drawable/normal_key_bg" android:state_pressed="false" />
<item android:drawable="@drawable/normal_key_bg" android:state_focused="false" />
<item android:drawable="@drawable/normal_key_bg" android:state_selected="false" />
<item android:drawable="@drawable/normal_key_bg" android:state_checkable="false" />
<item android:drawable="@drawable/normal_key_bg" android:state_checked="false" />
<!-- 按下状态下的透明度 -->
<item android:drawable="@drawable/normal_key_bg" android:state_pressed="true"
android:alpha="0.6" />
</selector>
```
在上面的代码中,我们定义了多个状态,包括普通状态、按下状态、选中状态等。对于每个状态,我们都设置了对应的背景图片,并使用 `android:alpha` 属性来设置透明度。当 Key 按下时,状态会变为按下状态,并且背景图片的透明度会变为 0.6。
通过上面的示例,您可以创建自定义的 Drawable 对象来设置 Keyboard 的 Key 背景样式,以实现不同的效果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)