我很难理解如何一起使用组合和接口来支持组合而不是继承?一个例子可以是:
界面:
public interface IMachine {
void TurnOn();
void TurnOff();
}
Run Code Online (Sandbox Code Playgroud)
Machine 类是 Printer 类的父类
public class Machine {
protected boolean isOn;
public Machine(boolean isOn) {
this.isOn = isOn;
}
public void TurnOn() {
isOn = true;
System.out.println("Machine is on !");
}
public void TurnOff() {
isOn = false;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建一个实现 IMachine 接口的打印机类,它将实现 IMachine 接口的方法。但是假设我创建了一个实现 IMachine 接口的 Clock 类,那么我必须再次实现这些方法。是否有更有效的方法来使用组合和接口并将方法委托给 Machine 类?
我正在尝试为在线拍卖系统制作类图,但遇到了这个问题。出价属于拍卖和买方(如果我错了,请纠正我)。那么我可以说 User 由 bid 组成,而 racing 由 Bid 组成,或者这违反了 UML 规则?我很困惑
我想更好地理解组合而不是继承。我看了一些教程,它们很好地解释了这些概念,但使用的是我更熟悉的语言,例如 Python。然而,我目前正在学习 C#,正在努力运用我在该主题上学到的知识,想知道是否有人可以提供帮助。这是我的代码:
using System;
namespace Composition
{
class Program
{
static void Main(string[] args)
{
SuperBot steven = new SuperBot(new Dog(),new Robot());
steven.Bark();
}
}
class Dog
{
public void Bark()
{
Console.WriteLine("Woof");
}
}
class Robot
{
public void Move()
{
Console.WriteLine("I'm moving!");
}
}
class CleanRobot
{
public void Clean()
{
Console.WriteLine("Just keep dusting, just keep dusting");
}
}
class SuperBot
{
// can clean, move and bark
public Dog o1;
public Robot o2;
public CleanRobot o3; …Run Code Online (Sandbox Code Playgroud) 我很难理解如何使用该包来处理基本情况,例如描述 API 请求/响应。我可能会陷入一个循环,我心里已经有了一些想法,我正试图不惜一切代价让它发挥作用,但我看不到更简单的解决方案。
例子:
@freezed
abstract class BaseRequest with _$BaseRequest {
const factory BaseRequest({
@required int a,
@required String b,
}) = _BaseRequest;
}
@freezed
abstract class BaseResponse with _$BaseResponse {
const factory BaseResponse({
@required bool c,
}) = _BaseResponse;
}
Run Code Online (Sandbox Code Playgroud)
然后
@freezed
abstract class Authentication with _$Authentication {
@Implements(BaseRequest)
const factory Authentication.request({
@required int a,
@required String b,
@required String psw,
}) = _AuthenticationRequest;
@Implements(BaseResponse)
const factory Authentication.response({
@required bool c,
@required String token,
}) = _AuthenticationResponse;
factory Authentication.fromJson(Map<String, …Run Code Online (Sandbox Code Playgroud) 我尝试使用组合与 lambda 表达式的虚拟示例。下面的代码可以编译,但当我尝试时它不会运行(f.g) 3 2
f x = x^2
g x = 5*x
f.g = \x -> f(g x)
Run Code Online (Sandbox Code Playgroud)
它给出了这样的错误:
Ambiguous occurrence `.'
It could refer to
either `Prelude..',
imported from `Prelude' at list3.hs:1:1
(and originally defined in `GHC.Base')
or `Main..', defined at list3.hs:42:3.
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我问题出在哪里吗?
我对 Vue3 或客户端开发总体来说有点陌生,我的任务是从 i18n 读取或更改全局区域设置。但根据文档,我应该能够在我的组件中使用 $i18n 变量,但它告诉我它是未定义的。
这是我的 i18n 配置:
从“vue-i18n”导入{createI18n};
从“./locales/en.json”导入 en;
从“./locales/nl.json”导入 nl;
常量 i18n = createI18n({
区域设置:“nl”,
允许成分:真,
warnHtmlInMessage: "关闭",
后备区域设置:“nl”,
消息:{
恩,
荷兰,
},
});
导出默认i18n;
我的 main.ts 正在使用它
从“vue”导入{createApp};
从“axios”导入 axios;
从“@/App.vue”导入应用程序;
从“@/router”导入路由器;
从“@/plugin/appSettings”导入 appSettings, { type iAppSettings };
从“@/plugin/api”导入 api;
从“@/i18n”导入 i18n;
从“@/stores”导入pinia;
const startUp = async () => {
尝试 {
让设置=空;
const 结果 = 等待 axios.get("config/config.json");
设置=结果.数据;
console.debug("加载的配置:", 设置);
const app = createApp(应用程序);
应用程序使用(pinia);
应用程序。使用(路由器);
app.use(appSettings, 设置);
app.use(api, 设置?.apiUrl);
应用程序.use(i18n);
app.mount("#app");
// … 我正在学习c ++教程并且很难理解以下代码的一部分(请参阅注释部分):
#include<iostream>
using namespace std;
class A
{
public:
A(A&src)
{
cout<<"copying A..."<<endl;
}
A(void){}
void Do(void)
{
cout<<"A is doing something"<<endl;
}
};
class B
{
public:
B(B&src)
{
cout<<"copying B..."<<endl;
}
B(void){}
void Do(void)
{
cout<<"B is doing something"<<endl;
}
};
class Compo
{
public:
Compo(Compo &src):f1(f1),f2(f2)//???
{
cout<<"Copying Compo..."<<endl;
}
Compo(void){}
A f1;
B f2;
};
int main(void)
{
Compo co1;
Compo co2=co1;
co2.f1.Do();
co2.f2.Do();
}
Run Code Online (Sandbox Code Playgroud)
那么编译器如何知道哪个f1/f2属于哪个Compo?有没有办法让它更明确?
谢谢您的帮助
如果已经提出这个问题我很抱歉.Show是一个非常常见的关键字,使我很难切断噪音.如果我有一个functor composition如下所示定义的类型,我无法弄清楚如何Show为该类型定义实例:
newtype FComp f g a = C { unC :: f (g a) }
--- Incomplete Show definition for FComp
instance Show (FComp f g a) where
show (C x) = "FComp" ++ show ??? --- Given a type say FComp Maybe Maybe Int, should print out "FComp Maybe Maybe Int"
Run Code Online (Sandbox Code Playgroud)
也:
$ :t show
show :: Show a => a -> String
Run Code Online (Sandbox Code Playgroud)
所以,似乎show接受一个值,并返回相应的字符串.堵x在show ???将无法工作,因为Show实例仍然需要对类型进行定义f …
以下函数是Yesod REST服务器的一部分,通过电子邮件地址在MongoDB数据库中搜索现有用户,并返回Maybe User:
{-# LANGUAGE DeriveGeneric #-}
module Model.User where
import Database.MongoDB (Action, findOne, select, (=:))
import qualified Database.MongoDB as M
import GHC.Generics (Generic)
import Import
data User = User
{ userEmail :: Text
, userFirstName :: Text
, userLastName :: Text
} deriving (Generic, Show)
collection :: Text
collection = "users"
instance FromJSON User
instance ToJSON User
findForEmail :: Text -> Action IO (Maybe User)
findForEmail email = do
maybeDocument <- findOne (select [ "email" =: email …Run Code Online (Sandbox Code Playgroud) 我已经定义了一个函数f1和f2,因此我可以使用函数composition(fkomp),它应该使用f1和f2来计算2^x给定List中的每个元素.
f1 :: Int -> Int
f1 x = product (replicate x 2)
f2 :: (a -> b) -> [a] -> [b]
f2 f xs = [f x | x <- xs]
fkomp :: [Int] -> [Int]
fkomp xs = f2 f1 $ xs
Run Code Online (Sandbox Code Playgroud)
它的工作原理,但问题是,我无法用组合编写我的代码:
fkomp xs = f2.f1 $ xs
Run Code Online (Sandbox Code Playgroud)
我一直在键入每一个组合,但它不适用于组合.
有人可以减轻我的道路吗?
非常感谢